0

I want to conditionally create a Binding of HTML node.

@dom def maybeEmpty: Binding[Node] = {
  if (math.random > 0.5) {
    <div>non-empty content</div>
  }
}

However the code does not compile.

error: type mismatch;
 found   : Unit
 required: org.scalajs.dom.raw.Node
Yang Bo
  • 3,586
  • 3
  • 22
  • 35

2 Answers2

1

Since Binding.scala 11.1.x you can write:

@dom def maybeEmpty: Binding[Option[Node]] = {
  if (math.random > 0.5) {
    Some(<div>non-empty content</div>)
  } else {
    None
  }
}
lmars
  • 302
  • 1
  • 6
0

You need an else block with an empty content, usually a HTML comment:

@dom def maybeEmpty: Binding[Node] = {
  if (math.random > 0.5) {
    <div>non-empty content</div>
  } else {
    <!-- empty content -->
  }
}
Yang Bo
  • 3,586
  • 3
  • 22
  • 35