在 Angular 应用程序中,我需要在 B.html
中包含 A.html
及其 Controller ,而不复制任何一个 A.html
或 Controller 。
我在A.html中写了下面的但是没用
<div ng-include src="src/html/v1/A.html"></div>
以下是应用程序的代码结构
src
├── html
│ ├── v1
│ │ ├── A.html
│ │ ├── B.html
├── index.coffee
├── index.html
├── js
│ ├── controllers
│ │ ├── v1
│ │ │ ├── AController.js
| │ │ ├── BController.js
└── sass
├── v1
以下是 index.coffee 包含的内容 -
$routeProvider
.when("/",
{
templateUrl: "src/html/v1/A.html",
title: "A"
controller: "AController"
})
.when("/A",
{
templateUrl: "src/html/v1/A.html",
title: "A"
controller: "AController"
})
.when("/B",
{
templateUrl: "src/html/v1/B.html",
title: "B"
controller: "BController"
})
.otherwise({ redirectTo: "/" })
请建议我在这里缺少什么。
PS:这个问题的答案可能是许多其他类似问题的答案,但我一开始找不到答案。对该问题的支持清楚地表明它具有相关性。
最佳答案
ng-include
的src
属性接受一个字符串。所以将其更改为:
<div ng-include src="'src/html/v1/A.html'"></div>
来自docs :
angular expression evaluating to URL. If the source is a string constant, make sure you wrap it in single quotes, e.g.
src="'myPartialTemplate.html'"
.
关于javascript - 无法在同一 HTML 中使用多个模板/ Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37371560/