【问题标题】:Unable to use multiple templates/controllers in same HTML无法在同一个 HTML 中使用多个模板/控制器
【发布时间】:2016-09-19 04:29:24
【问题描述】:

在 Angular 应用程序中,我需要在 B.html 中包含 A.html 及其控制器,而不需要复制 A.html 或 AController。

我在 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:这个问题的答案可能是许多其他类似问题的答案,但我一开始找不到答案。对这个问题的支持清楚地表明它具有相关性。

【问题讨论】:

    标签: javascript html angularjs angularjs-ng-include


    【解决方案1】:

    ng-includesrc 属性接受字符串。所以改成:

    <div ng-include src="'src/html/v1/A.html'"></div>
    

    来自docs

    角度表达式评估为 URL。如果源是字符串 常量,请确保将其括在单引号中,例如 src="'myPartialTemplate.html'".

    【讨论】:

    • 在旁注中,此机制不包括控制器。仅包含 HTML 模板。
    【解决方案2】:

    应该是&lt;div ng-include src="'src/html/v1/A.html'"&gt;&lt;/div&gt;

    【讨论】:

    • 当您首先发布答案时,我已向您投了赞成票,但我已将 shashank-agrawal 的答案标记为正确,因为它得到了更好的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2021-04-06
    • 2012-08-09
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多