如何在单个块中使用Scala和HTML代码 [英] How to use scala and html code inside single block

查看:206
本文介绍了如何在单个块中使用Scala和HTML代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么情况1中的html元素选项未绑定在select中?

why option html element is not binded inside select in case 1?

情况1:不起作用

@base{
  <select name="" value="" class="custom-select">
  @{
    println("1"); // this is printed to console             
    <option value="test">i</option> // this is not shown in html
    println("2"); // this is printed to console                     
  }
  </select>
}

案例2:工作

@base{
  <select name="" value="" class="custom-select">
  @{
    println("1"); // this is printed to console             
    <option value="test">i</option> // this is shown in html                    
  }
  </select>
}

更新:

如何创建一个将所有选项元素绑定到scala模板的循环?以下代码不绑定任何选项元素.实际的返回类型是什么?空行?

How one can create a loop which binds all option elements to scala template? Following code does not bind any option elements. What is actually return type? Empty line?

<select name="" value="" class="custom-select">
@{
    for(i <- 1 to 10) {
        <option value="@i">@i</option>
    }
}
</select>

推荐答案

代码块@{...}是一个闭包,具有从最后一条语句推断出的返回类型.

The code block @{...} is a closure that has an inferred return type from the last statement.

在第一种情况下,由于println(...)返回Unit

In the first case the return type is inferred to be Unit since the println(...) returns Unit

在第二个代码段中,返回html.

In the second block the html is returned.

这篇关于如何在单个块中使用Scala和HTML代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆