如何设置Grails和AngularJS部分模板

How to setup Grails and AngularJS partial templates

本文关键字:AngularJS Grails 何设置 设置      更新时间:2023-09-26

我正在实现一个使用AngularJS作为前端和Grails作为后端的项目。

  • 角度 JS => 单页应用程序
  • Grails => REST API,用于WebApp本身和第三方应用程序。

这是我设置项目的方式:

web-app
   |
   |_____ js ( angular controllers, modules, partial templates.)
   |
   |_____ images
   |
   |_____ css

grails-app
   |
   |_____ views ( in here I have my main view, the one I use at the first user request )

与其使用资源插件,我更喜欢使用 Grunt 构建自己的前端,然后我只在布局本身内链接最终文件。

我在 web 应用程序中构建了js文件夹,以包含一个partials文件夹,其中包含要在 AngularJS 中调用的所有部分模板

这是我非常标准的角度代码来加载视图:

angular.module('myapp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/invoices', {
        templateUrl: 'partials/invoices-list.html',   
        controller: InvoiceListCtrl
    })
    .when('/invoices/:invoiceId', {
        templateUrl: 'partials/invoice-details.html', 
        controller: InvoiceDetailCtrl}
    )
    .otherwise({redirectTo: '/dashboard'}, {
        templateUrl: 'partials/dashboard.html', 
        controller: DashboardCtrl
    });
}]);

发生的情况是 Angular 无法获取这些部分模板,因为部分文件夹未复制到 tomcat work 目录中。

我不知道哪种其他方法可用于Grails驱动的项目。

我相信问题是静态资源不是从默认的 grails-app 目录衍生而来的,所以你需要包含来自文档根目录的完整路径,例如 templateUrl: '/partials/invoices-list.html',

这是我对ui路由器的设置:

App.config([
  '$stateProvider'
  '$urlRouterProvider'
  ($stateProvider, $urlRouterProvider) ->
    $urlRouterProvider.otherwise("/")
    $stateProvider
      .state('intro', {
        url: "/",
        templateUrl: '/templates/intro.html'
      })

我通过标准的 ajax 请求提供我的部分/视图。

class ViewController {
  def invoiceList() {
    render(template: 'invoiceList')
  }
  ...
}
$routeProvider
    .when('/invoices', {
        templateUrl: contextPath + '/view/invoiceList',   
        controller: InvoiceListCtrl
    })

contextPath派生自我存储在主 GSP 的全局变量中的${request.contextPath}

默认情况下,web-app文件夹中的所有文件都将复制到war中名为static的文件夹中。(您可以更改此行为。请参阅 Grails 文档)。

在您的示例中,您可以检查以下 URL 中的部分文件。

http://localhost:8080/yourApp/static/js/partials/invoices-list.html
http://localhost:8080/yourApp/static/js/partials/invoices-details.html
http://localhost:8080/yourApp/static/js/partials/dashboard.html

因此,您需要做的是找出此文件的路径。像这样:

angular.module('myapp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/invoices', {
        templateUrl: '../static/js/partials/invoices-list.html',   
        controller: InvoiceListCtrl
    })
    .when('/invoices/:invoiceId', {
        templateUrl: '../static/js/partials/invoice-details.html', 
        controller: InvoiceDetailCtrl}
    )
    .otherwise({redirectTo: '/dashboard'}, {
        templateUrl: '../static/js/partials/dashboard.html', 
        controller: DashboardCtrl
    });
}]);

请务必注意,templateUrl路径不应与 JS 文件相关。

再比如:

grails-app
    views
        mydomain
            index.gsp
web-app
    js
        partials
            mydomain-detail.html
            mydomain-list.html
        controllers
            mydomain-controllers.js

参考资料:

http://localhost:8080/myApp/mydomain/  
http://localhost:8080/myApp/static/js/partials/mydomain-detail.html  
http://localhost:8080/myApp/static/js/partials/mydomain-list.html  

路由配置:

angular.module('myapp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/list', {
        templateUrl: '../static/js/partials/mydomain-detail.html',   
        controller: MyDomainListCtrl
    })
    .when('/show/:id', {
        templateUrl: '../static/js/partials/mydomain-detail.html', 
        controller: MyDomainDetailCtrl}
    )
    .otherwise({
         redirectTo: '/list'
    });
}]);

作为詹姆斯答案的替代方案,您可以直接使用GSP。在另一个线程中查看我的答案。

关于contextPath:我会说,如果您添加前面的斜杠,您会自动获得正确的上下文。

templateUrl: 'view/invoiceList'

此致敬意比约恩

附言很抱歉将此添加为完整答案,我更希望将其作为对詹姆斯的评论。但是,系统拒绝我添加评论:-(