mty*_*urt 5 javascript spring thymeleaf
我正在尝试编写一些我需要使用模型属性的Javascript代码.以下是我定义脚本标记的方法:
<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
//need some loops
/*]]>*/
</script>
Run Code Online (Sandbox Code Playgroud)
我需要做的是,each在脚本中使用模型属性的迭代.到目前为止,我无法做到这一点th:each.任何帮助表示赞赏.
snw*_*snw 18
我想你需要用双括号包装模型attrubite,如下所示:[[${modelAttribute}]].Thymeleaf文档的脚本内联部分可以提供一些帮助:http:
//www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#script-inlining-javascript-and-dart
<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
var theList = [[${modelAttribute}]]
for (i = 0; i < theList.length; i++) {
doSomething(theList[i]);
}
/*]]>*/
</script>
Run Code Online (Sandbox Code Playgroud)
ygl*_*odt 11
你也可以这样做,这是我认为最紧凑的:
在你的@Controller:
model.addAttribute("items", new String[] { "item1", "item2", "item3" });
Run Code Online (Sandbox Code Playgroud)
在您的模板中:
<script type="text/javascript" th:inline="javascript">
var items = [];
/*[# th:each="n : ${items}"]*/
items.push("[(${n})]");
/*[/]*/
</script>
Run Code Online (Sandbox Code Playgroud)
其他有用的东西在这里解释:[MAJOR FEAT]文本模板模式的新语法#395
| 归档时间: |
|
| 查看次数: |
17130 次 |
| 最近记录: |