angular-rails-templates: templates are not found

一个人想着一个人 提交于 2019-11-30 19:24:51

问题


I have a rails application, using angularjs for the client side development. I try to load a template located in `app/assets/javascripts/templates/':

myApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: '/index.html',
        controller: 'MainController'
      }).
      otherwise({
        redirectTo: '/'
      });
  }]);

but I always get a error: "Error: [$compile:tpload] Failed to load template: /index.html".

I tried to move the "templates" folder out of the javascripts - app/assets/templates and to add it to my assets pipeline load by adding the following line to config/application.rb: config.assets.paths << Rails.root.join("app","assets","templates")

I keep getting the same error message until I use the full path to the template: templateUrl: '/assets/index.html',.

  1. Why the first method is not enough? the angular-rails-templates gem shouldn't look for templates in app/assets/javascripts/templates?

  2. Why should I use the full path to an assets inside my javascript?


回答1:


My problem has been a sprockets incompatibility. Version 2.1.3 works though so I put this in my Gemfile:

gem 'sprockets', '2.12.3'

And I ran bundle update sprockets and it's all peachy.




回答2:


Practice has shown that you should keep Angular templates in ./public folder. For example make a folder called templates in the public folder. Now you can just call them like this: templateUrl: 'templates/index.html'

This makes sense as the app/assets/ is indeed only used for javascript and css files. Also you have the html files in app/views, but these are compiled by server because they use .erb extension. What you want is to have regular html files ready to be used any time. So you put them to public folder.




回答3:


try this, I hope this will worked.

 myApp.config(function ($routeProvider) {
  $routeProvider.
    when('/', {
     templateUrl: '/index.html',
     controller: 'MainController'
    }).
    otherwise({
     redirectTo: '/'
   });
});



回答4:


I solved this issue by adding "gem 'sprockets'" into the Gemfile



来源:https://stackoverflow.com/questions/29749099/angular-rails-templates-templates-are-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!