【问题标题】:AngularJS. ng-include inside of ng-view infinitely loads content of ng-viewAngularJS。 ng-view 内的 ng-include 无限加载 ng-view 的内容
【发布时间】:2016-03-26 04:36:49
【问题描述】:

这是ng-includeng-view 输出中的结果:

当然,我只需要加载这个页面一次。

我的 app.js:

var app = angular.module('app', ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
    $routeProvider

    .when('/first_route', {
        templateUrl: '/templates/first/index.html',
        controller: 'FirstController',
        controllerAs: 'First',
    })
    .when('/second_route', {
        templateUrl: '/templates/second/index.html',
        controller: 'SecondController',
        controllerAs: 'Second',
    });

// I omitted my app.run(), because it contains only headers and redirect to 
// login page for not authenticated users

};

在我的主要index.html 文件中,我只使用ng-view 指令。

我的first/index.html 包含:

<h1>Current page</h1>
<ng-include src="'first/show.html'"></ng-include>
// remaining part of page

我的first/show.html 包含:

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

继续:我尝试按下一个顺序加载模板:

  1. index.html (ng-view)
  2. first/index.html(ng-include)
  3. first/show.html(ng-include)

如何解决这个问题?

【问题讨论】:

  • 你能用一个小程序重建这个问题吗?我在提供的代码/标记中看不到任何错误。
  • @null 我已将我的代码添加到 plunker,它工作正常 (plnkr.co/edit/XaPLhjQPUBgZZryzFW1U?p=preview)。看不懂,本地服务器的代码有什么问题,继续调试。

标签: angularjs ng-view angularjs-ng-include


【解决方案1】:

我解决了这个问题。

问题是路径不正确,所以当ng-include找不到模板时,它会无限加载自己。

【讨论】:

  • 说明正确的路径是什么,以便未来的读者可以看到您的问题是非常有帮助的。
【解决方案2】:

对于可能遇到类似问题的人,请确保 first/index.htmlfirst/show.html 包含您要显示的内容(不是空白页面),否则您可能也有与上述相同的循环!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2014-09-29
    • 2016-07-26
    • 2013-06-05
    • 2016-01-04
    • 1970-01-01
    • 2023-03-19
    • 2022-01-24
    相关资源
    最近更新 更多