javascript - 从 thymeleaf 获取对象到 js

标签 javascript spring spring-boot thymeleaf

我有一个像这样的for:each:

<tbody>
    <tr th:each="bill : ${bills}" id="bill">
        <td th:text="${bill.bill_id}"></td>
        <td th:text="${bill.client.phone}"></td>
        <td th:text="${bill.date}"></td>
        <td><a href="#" class="btn btn-info btn-xs" data-toggle="modal" id="open-modal">Ver detalles</a></td>
    </tr>
</tbody>

问题是我如何将整个 ${bill} 获取到 js 或 jquery 中的变量,因为我需要更多信息,我看到了文档,它说:

<script th:inline="javascript">
/*<![CDATA[*/
    ...

    var user  name = /*[[${bill}]]*/ '';

    ...
/*]]>*/
</script>

但是我在外部 js 文件上尝试过这个方法,但不起作用,所以我需要 1 个解决方案。

imagen del error

最佳答案

您可以将 ${bills} 作为 JavaScript 对象,试试这个

<script th:inline="javascript">
/*<![CDATA[*/

    var bills = [[${bills}]];  //Make sure this is bills and not bill as you have done it.
    console.log(bills);

/*]]>*/
</script>

关于javascript - 从 thymeleaf 获取对象到 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47246090/

相关文章:

javascript - HandlebarsJS : Get elements i of each array, 然后元素 j 等?

javascript - 在ajax中读取对象数组的数组

spring-boot - 无法使Netbeans Project确认现有的Tomcat或Glassfish服务器

java - DispatcherServletRegistration Spring启动异常

javascript - 自己的 reducer 中的空状态

javascript - 使用 jquery 向左滚动动画

java - 无法集成测试 gradle 多模块 Spring-Boot-Application

spring - Spring Security 中使用编码密码进行身份验证

java - 使用 Spring Data Core 中的 PagedResourcesAssembler 进行 HATEOAS 链接生成

tomcat - Spring boot - 无法更改 TomCat 上的默认 8080 端口