【问题标题】:Angulars + Rails: ngInclude directive not workingAngularjs + Rails:ng Include指令不起作用
【发布时间】:2014-12-07 10:35:45
【问题描述】:

我正在开发一个 rails + angularjs 应用程序。

这是我的文件结构:

app/
assets/
    javascript/
    templates/
       home.html
       partial.html

在 home.html.erb 文件中,我想包含 partial.html.erb 文件。

home.html.erb

<ng-include src="'partial.html'"></ng-include>

我也试过

<ng-include src="'<%= asset_path('partial.html') %>'"></ng-include>

但还是不行……谢谢你的帮助

【问题讨论】:

    标签: ruby-on-rails angularjs templates partial-views angularjs-ng-include


    【解决方案1】:

    混合服务器模板和 AngularJS 的模板不是一个好主意。将您的 AngularJS 模板放在您的公共目录中,然后在 src 属性中放入来自客户端的此模板的路径。

    public/templates/partial.tpl.html => &lt;ng-include src="'/templates/partial.tpl.html'&gt;&lt;/ng-include&gt;"

    从客户端获取模板的另一种方法是将模板编译为带有html2js 的JS 文件。

    【讨论】:

      【解决方案2】:

      Setup gem "angular-rails-templates"

      它会自动将您的模板编译成 javascript,并使它们可用于 Angular。之后,您可以像往常一样使用 ng-include。

      <ng-include src="'templates/partial.tpl.html'"></ng-include>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        相关资源
        最近更新 更多