how to use AngularJS routes with Haml templates in a Rails app how to use AngularJS routes with Haml templates in a Rails app angularjs angularjs

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


I got something working without $templateCache. I made a config/initializers/haml.rb file with the following:

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').

I also renamed my template file from api-list.haml to 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.


The reason you were having trouble with templateCache is that routes are in a config block. If you refer to the AngularJS documentation about modules, you will see the following line:

Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.