【问题标题】:can I use angular2 and Thymeleaf together?我可以一起使用 angular2 和 Thymeleaf 吗?
【发布时间】:2017-03-13 14:15:09
【问题描述】:

我将 Thymeleaf th: 放在 angular2 模板中,但 th: 不会编译。有什么办法可以一起使用吗?

import {Component, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'


@Component({
     selector: 'my-app',
     template: `
     <table xmlns:th="http://www.w3.org/1999/xhtml">
         <tbody>
            <tr>
                <th>User Name</th>
                <th th:each="role : ${roles}">
                   <div class="row">
                       <div th:text="${role.type}"></div>
                   </div>
                </th>
            </tr>
        </tbody>
    </table>`,
    })
export class App {}

【问题讨论】:

    标签: angularjs spring-mvc angular thymeleaf angular2-template


    【解决方案1】:

    您将两个前端框架混合在一起。 Angular 是在客户端处理的前端框架,而 Thymeleaf 是在服务器端处理的。

    你有两个选择:

    • 同时使用 Angular 和(例如)Spring,您将在其中使用 REST API 将数据从后端公开到 Angular(前端),然后 您将使用*ngFor 指令。

    • 将 Thymeleaf 与 Spring MVC 或 Spring Webflow 等框架结合使用 等等(也可以在这里使用 Angular,但你必须知道 Thymeleaf 是在服务器端处理的,所以 你不能把 Thymeleaf 代码到 JavaScript 文件并期望它得到 已处理)。

    【讨论】:

      【解决方案2】:

      是的,您可以同时使用 Angular 和 Thymeleaf。在您的代码中,您已将布局信息放入 javascript,这不是一个好习惯;你应该把它从 javascript 代码中提取出来,并使其成为一个 html 模板。您需要注意放置 html 模板的位置。请在此处查看我的帖子:

      Make Spring Thymeleaf work with AngularJS routeProvider

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-28
        • 2020-04-10
        • 1970-01-01
        • 2018-03-20
        相关资源
        最近更新 更多