[英]How to use pattern matching in Binding.scala?
我有以下模型:
case class CarBinding(ownerId: Var[String], specs: Var[Option[Specs]])
Specs
是一個特征,具有以下具體類型:
trait Specs {
def name: String
}
case class SportsCarSpecs(name: String, details: Details) extends Specs
現在,在我的Scala.js應用程序中,我想創建一個表並列出所有條目:
@dom
def buildTable(): Binding[BindingSeq[Node]] = {
val data = Vars.empty[CarBinding]
/* Initial population. */
// Some code...
<br/>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th class="col-md-1">
<small>Owner ID</small>
</th>
<th class="col-md-1">
<small>Specs</small>
</th>
</tr>
</thead>
<tbody>
{for (entry <- data) yield {
<tr>
<td>
<small>
{entry.ownerId.bind}
</small>
</td>
<td>
<small>
{entry.specs.bind match {
case Some(SportsCarSpecs(name, details)) => {name} <span>{details.ps}</span>
case _ => -
}}
</small>
</td>
</tr>
}}
</tbody>
</table>
</div>
}
但是,出現以下錯誤:
';' expected but $XMLSTART$< found.
[error] case Some(SportsCarSpecs(name, details)) => {name} <span>{details.ps}</span>
我究竟做錯了什么?
這個:
entry.specs.bind match {
case Some(SportsCarSpecs(name, details)) => {name} <span>{details.ps}</span>
case _ => -
}
不是有效的表達式,因此您不能在XML文字中插入它。 如果您在兩個分支中都有完整的XML表達式,它應該可以工作。 因此,我看到的最簡單的修復方法是將<small>
拉入內部:
<td>
{entry.specs.bind match {
case Some(SportsCarSpecs(name, details)) => <small>{name} <span>{details.ps}</span></small>
case _ => <small>-</small>
}}
</td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.