简体   繁体   English

如何在Rails应用程序中使用带有Haml模板的AngularJS路由

[英]how to use AngularJS routes with Haml templates in a Rails app

I have app/assets/templates/api-list.haml that I want to use as an AngularJS template. 我有app / assets / templates / api-list.haml我想用作AngularJS模板。 I followed this tutorial to create a templates.js file that inserts all my compiled Haml AngularJS templates into $templateCache . 我按照本教程创建了一个templates.js文件,该文件将我编译的所有Haml AngularJS模板插入到$templateCache However, I can't seem to make these cached templates work with my AngularJS routes: 但是,我似乎无法使这些缓存模板与我的AngularJS路由一起使用:

api_app.config(['$routeProvider', ($routeProvider) ->
  $routeProvider.when '/',
    templateUrl: 'api-list'
    controller: api_app.ApiListController
  $routeProvider.otherwise
    redirectTo: '/'
])

When I load my app, I see a 404 error in the browser console because it tried to do a request to http://localhost:3000/api-list . 当我加载我的应用程序时,我在浏览器控制台中看到404错误,因为它尝试向http://localhost:3000/api-list发出请求。 I can look at /assets/templates.js in the browser and see that $templateCache.put("api-list" is defined, so there should be a template called "api-list". I am loading templates.js in my page before defining routes. 我可以在浏览器中查看/assets/templates.js并看到$templateCache.put("api-list"已定义,因此应该有一个名为“api-list”的模板。我在我的模板中加载templates.js定义路线前的页面。

I also tried injecting $templateCache in my route config like so: 我也尝试在我的路由配置中注入$templateCache ,如下所示:

api_app.config(['$routeProvider', ($routeProvider, $templateCache) ->
  $routeProvider.when '/',
    template: $templateCache.get('api-list')
    controller: api_app.ApiListController
  $routeProvider.otherwise
    redirectTo: '/'
])

This causes an Uncaught TypeError: Cannot call method 'get' of undefined from ApiApp error though. 这会导致Uncaught TypeError: Cannot call method 'get' of undefined from ApiApp错误中Uncaught TypeError: Cannot call method 'get' of undefined from ApiApp If I change the first line to api_app.config(['$routeProvider', '$templateCache', ($routeProvider, $templateCache) -> , I instead get the error Uncaught Error: Unknown provider: $templateCache from ApiApp . 如果我将第一行更改为api_app.config(['$routeProvider', '$templateCache', ($routeProvider, $templateCache) -> ,我会Uncaught Error: Unknown provider: $templateCache from ApiApp获取错误Uncaught Error: Unknown provider: $templateCache from ApiApp

How can I convince my routes to use the template from $templateCache instead of trying to load it via a new request? 如何说服我的路由使用$templateCache而不是尝试通过新请求加载它?

I got something working without $templateCache . 我有一些没有$templateCache东西。 I made a config/initializers/haml.rb file with the following: 我使用以下命令创建了config / initializers / haml.rb文件:

Rails.application.assets.register_engine '.haml', Tilt::HamlTemplate

Then in my config/application.rb, I added config.assets.paths << Rails.root.join('app', 'assets', 'templates') . 然后在我的config / application.rb中,我添加了config.assets.paths << Rails.root.join('app', 'assets', 'templates')

I also renamed my template file from api-list.haml to api-list.html.haml . 我还将我的模板文件从api-list.haml重命名为api-list.html.haml My routes look like this now: 我的路线现在看起来像这样:

api_app.config(['$routeProvider', ($routeProvider) ->
  $routeProvider.when '/',
    templateUrl: '/assets/api-list.html'
    controller: api_app.ApiListController
  $routeProvider.otherwise
    redirectTo: '/'
])

I don't like having /assets hard-coded in there, so I may end up changing this file from routes.js.coffee to routes.js.erb and using Rails helpers to get a path. 我不喜欢在那里使用/assets硬编码,所以我最终可能会将这个文件从routes.js.coffee更改为routes.js.erb并使用Rails帮助程序来获取路径。

The reason you were having trouble with templateCache is that routes are in a config block. 你遇到templateCache问题的原因是路由在配置块中。 If you refer to the AngularJS documentation about modules , you will see the following line: 如果您参考有关模块的AngularJS文档,您将看到以下行:

Only providers and constants can be injected into configuration blocks. 只有提供程序和常量才能注入配置块。 This is to prevent accidental instantiation of services before they have been fully configured. 这是为了防止在完全配置服务之前意外实例化服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM