我有要用作AngularJS模板的app / assets / templates / api-list.haml。我按照本教程创建了一个templates.js文件,该文件将我所有已编译的Haml AngularJS模板插入$templateCache
。但是,我似乎无法使这些缓存的模板与我的AngularJS路由一起使用:
api_app.config(['$routeProvider', ($routeProvider) ->
$routeProvider.when '/',
templateUrl: 'api-list'
controller: api_app.ApiListController
$routeProvider.otherwise
redirectTo: '/'
])
加载应用程序时,我在浏览器控制台中看到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: '/'
])
但这会导致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
然后在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: '/'
])
我不喜欢在其中进行/assets
硬编码,因此最终可能会将此文件从routes.js.coffee更改为routes.js.erb,并使用Rails帮助器获取路径。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句