angular-rails-templates:找不到模板
我有一个rails应用程序,使用angularjs进行客户端开发。 我尝试加载位于`app / assets / javascripts / templates /’的模板:
myApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/', { templateUrl: '/index.html', controller: 'MainController' }). otherwise({ redirectTo: '/' }); }]);
但我总是收到一个错误:“错误:[$ compile:tpload]无法加载模板:/index.html”。
我试图将“templates”文件夹移出javascripts – app / assets / templates并将其添加到我的资产管道加载中,方法是config.assets.paths << Rails.root.join("app","assets","templates")
添加到config / application.rb: config.assets.paths << Rails.root.join("app","assets","templates")
在我使用模板的完整路径之前,我一直收到相同的错误消息: templateUrl: '/assets/index.html',
-
为什么第一种方法不够用? angular-rails-templates gem不应该在app / assets / javascripts / templates中查找模板?
-
为什么我要使用javascript中的资源的完整路径?
我的问题是sprockets
不兼容。 版本2.1.3工作,所以我把它放在我的Gemfile
:
gem 'sprockets', '2.12.3'
我运行bundle update sprockets
,它都是桃子。
实践certificate,您应该将Angular模板保存在./public
文件夹中。 例如,在公用文件夹中创建一个名为templates的文件夹。 现在你可以像这样调用它们: templateUrl: 'templates/index.html'
这是有道理的,因为app/assets/
确实只用于javascript和css文件。 你也有app/views
的html文件,但这些是由服务器编译的,因为它们使用.erb
扩展名。 你想要的是随时准备好使用常规的html
文件。 所以你把它们放到公共文件夹。
试试这个,我希望这会奏效。
myApp.config(function ($routeProvider) { $routeProvider. when('/', { templateUrl: '/index.html', controller: 'MainController' }). otherwise({ redirectTo: '/' }); });
我通过在Gemfile中添加“gem’sprockets’”解决了这个问题