也许您在相同的方法中定义了<input ...>
and而is after 。尝试将and重构为单独的方法或让表达式嵌套在另一个 DOM 中。.bind
@dom
<input ...>
.bind
.bind
<input ...>
@dom
.bind
例如,如果myInput
是之前写的.bind
,它不会被重新创建:
import com.thoughtworks.binding._, Binding._
import org.scalajs.dom._
@dom def render = {
val color = Var("")
val myInput = <input id="myInput" type="text" oninput={ _: Any => color := myInput.value }/>
val styleText: String = s"background-color: ${color.bind}"
// <div> will be recreated once data changes.
// <input> will not be recreated.
<div style={styleText}>
{myInput}
</div>
}
dom.render(document.body, render)
上面的示例在ScalaFiddle上运行。
如果.bind
表达式嵌入在 XHTML 文字中,则不会影响其子项:
import com.thoughtworks.binding._, Binding._
import org.scalajs.dom._
@dom def render = {
val color = Var("")
// <div> and <input> will not be recreated when data changes.
// Only the class attribute will be changed.
<div style={s"background-color: ${color.bind}"}>
<input id="myInput" type="text" oninput={ _: Any => color := myInput.value }/>
</div>
}
dom.render(document.body, render)
上面的示例在ScalaFiddle上运行。
.bind
另一种方法是将表达式包装在 a 中@dom val
:
import com.thoughtworks.binding._, Binding._
import org.scalajs.dom._
@dom def render = {
val color = Var("")
@dom val styleText: Binding[String] = s"background-color: ${color.bind}"
// <div> and <input> will not be recreated when data changes.
// Only the class attribute will be changed.
<div style={styleText.bind}>
<input id="myInput" type="text" oninput={ _: Any => color := myInput.value }/>
</div>
}
dom.render(document.body, render)
上面的示例在ScalaFiddle上运行。