【发布时间】:2015-07-30 20:28:01
【问题描述】:
我已经使用 this guide 将 Angular 引导到我的 Rails 项目中
在控制台中,我得到了一个“正在运行的 exampleApp”,所以 Angular 正在运行。下一步是尝试在索引文件中包含一个模板。
我已将此添加到 index.html.erb
<div ng-include="'templates/header.html'"></div>
但它不包括模板。
这是我的结构
- 应用程序
- 资产
- javascript
- 角应用
- app.js.coffee
- 模板
- 角应用
- javascript
- 资产
这是我的 app.js.coffee 文件的内容
@app = angular.module('app', [
# additional dependencies here, such as restangular
'templates'
])
# for compatibility with Rails CSRF protection
@app.config([
'$httpProvider', ($httpProvider)->
$httpProvider.defaults.headers.common['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content')
])
@app.run(->
console.log 'angular app running'
)
但我没有在控制台中看到“angular app running”消息,所以我认为这个文件没有做任何事情。
对问题所在有什么想法吗?
// 编辑。
这是我的 index.html
<div ng-app='app.exampleApp' ng-controller='ExampleCtrl'>
<p>Value from ExampleCtrl:</p>
<p>{{ exampleValue }}</p>
</div>
<div ng-include="'templates/header.html'"></div>
我意识到我没有在 ng-app 中加载模板,所以很明显它不起作用。
我在 ng-app 中移动了包含,现在我收到 404 错误。所以至少它试图包含该文件。
//编辑2
通过将模板的路径更改为<div ng-include="'assets/angular-app/templates/header.html'"></div>,它包含了文件。
【问题讨论】:
-
打开控制台,你看到了什么,404为模板请求?
-
控制台中的 ng-include 命令没有反馈。在 HTML 中,它显示为空的 ''。就像我说的那样,问题似乎出在路由模板的 app.js.coffe 文件上,因为如果我删除该文件,项目中没有任何变化。
-
模板路径应该相对于您的索引页面。什么是索引页路径?
-
对,模板文件夹与您的索引文件所在的文件夹相同吗?
-
没有模板文件夹在 angular-app 文件夹内。 app.js.coffe 文件(包括模板路径)也在 angular-app 文件夹中。