当前位置: 动力学知识库 > 问答 > 编程问答 >

Rails 4 + AngularJS: templateUrl for directive not found?

问题描述:

I'm trying to create a directive in AngularJS to split my HTML into reusable partials:

app/assets/javascripts/products/directives/products.js:

app.directive('productsfilter', function() {

return {

restrict: 'E',

templateUrl: "/templates/filter-template.html"

}

});

However, the templates are not found:

Error: [$compile:tpload] Failed to load template: /templates/filter-template.html http://errors.angularjs.org/1.2.20/$compile/tpload?p0=%2Ftemplates%2Ffilter-template.html

I also tried ../templates/filter-template.html and templates/filter-template.html but neither worked. Is it because of Rails's asset pipeline?

Here's my folder tree from within app/assets/javascripts:

└── products

├── controllers

│ ├── mens_controller.js

│ ├── productscontroller.js

│ ├── womens_eyeglasses_controller.js

│ └── womens_sunglasses_controller.js

├── directives

│ └── products.js

├── filters

│ └── productsfilters.js

├── services

│ └── productsservices.js

└── templates

└── filter-template.html

网友答案:

I've encountered same issue. My path to my template was:

assets/templates/partials/question/multipleChoice.html

when I moved the template into:

assets/templates/multipleChoice.html
the embedded:
<%= asset_path('multipleChoice.html') %>
worked. Perhaps, it is the limitation of
asset_path()
method to dig much deeper.

To make the method be able to find the template, I pointed it out, like this:

templateUrl: "<%= asset_path('partials/question/multipleChoiceSingleAnswer.html') %>"
and it worked. (of course, you have to change the extension to .js.erb).

I don't want to place my templates inside the public folder. I want Rails to handle it for me and make it dynamic.

网友答案:
app.directive('productsfilter', function() {
  return {
    restrict: 'E',
    templateUrl: "filter-template.html"
  }
});

Use this gem if you are not using it.

gem 'angular-rails-templates'

Worked for me.

分享给朋友:
您可能感兴趣的文章:
随机阅读: