Sar*_*els 4 javascript ruby-on-rails angularjs ruby-on-rails-4
我有app/assets/templates/api-list.haml我想用作AngularJS模板.我按照本教程创建了一个templates.js文件,该文件将我编译的所有Haml AngularJS模板插入其中$templateCache.但是,我似乎无法使这些缓存模板与我的AngularJS路由一起使用:
api_app.config(['$routeProvider', ($routeProvider) ->
$routeProvider.when '/',
templateUrl: 'api-list'
controller: api_app.ApiListController
$routeProvider.otherwise
redirectTo: '/'
])
Run Code Online (Sandbox Code Playgroud)
当我加载我的应用程序时,我在浏览器控制台中看到404错误,因为它尝试执行请求http://localhost:3000/api-list.我可以在浏览器中查看/assets/templates.js并查看是否$templateCache.put("api-list"已定义,因此应该有一个名为"api-list"的模板.我在定义路由之前在我的页面中加载templates.js.
我也试过$templateCache像我这样注入我的路由配置:
api_app.config(['$routeProvider', ($routeProvider, $templateCache) ->
$routeProvider.when '/',
template: $templateCache.get('api-list')
controller: api_app.ApiListController
$routeProvider.otherwise
redirectTo: '/'
])
Run Code Online (Sandbox Code Playgroud)
这会导致Uncaught TypeError: Cannot call method 'get' of undefined from ApiApp错误.如果我将第一行更改为api_app.config(['$routeProvider', '$templateCache', ($routeProvider, $templateCache) ->,我会得到错误Uncaught Error: Unknown provider: $templateCache from ApiApp.
如何说服我的路线使用模板$templateCache而不是尝试通过新请求加载它?
我没有工作$templateCache.我使用以下命令创建了config/initializers/haml.rb文件:
Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate
Run Code Online (Sandbox Code Playgroud)
然后在我的config/application.rb中,我添加了config.assets.paths << Rails.root.join('app', 'assets', 'templates').
我还将模板文件重命名api-list.haml为api-list.html.haml.我的路线现在看起来像这样:
api_app.config(['$routeProvider', ($routeProvider) ->
$routeProvider.when '/',
templateUrl: '/assets/api-list.html'
controller: api_app.ApiListController
$routeProvider.otherwise
redirectTo: '/'
])
Run Code Online (Sandbox Code Playgroud)
我不喜欢/assets在那里进行硬编码,所以我最终可能会将这个文件从routes.js.coffee更改为routes.js.erb并使用Rails助手来获取路径.
| 归档时间: |
|
| 查看次数: |
3752 次 |
| 最近记录: |