동일한 @dom
방법에서 <input ...>
및 .bind
을 정의했을 수 있으며 <input ...>
은 .bind
이후에 정의했을 수 있습니다. .bind
및 <input ...>
을 각각 @dom
메서드로 리팩토링하거나 .bind
표현식을 다른 DOM에 중첩 시키십시오.
는 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에서 실행됩니다.
다른 접근법은 @dom val
에 .bind
식 배치되어
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.
이 질문과 답변은 https://github.com/ThoughtWorksInc/Binding.scala/wiki/FAQ에서 옮겨졌습니다. –