2

我正在尝试在以下内容中动态加载模板ng-repeat

<ul>
    <li ng-repeat="prop in entity" >
        <div ng-include src="prop.template"></div>
    </li>
</ul>

prop.template等于模板的 URL,例如'partials/form/text.html'. 上面的代码产生以下错误:

Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["prop.template; newVal: \"partials/form/text.html\"; oldVal: undefined","prop.template; newVal: \"partials/form/text.html\"; oldVal: undefined" .... (and so on)

如何prop.template从该文件中获取模板的 URL 并将 HTML 插入到 DOM 中ng-repeat

4

2 回答 2

2

http://docs.angularjs.org/api/ng.$rootScope.Scope#$digest

Process all of the watchers of the current scope and its children. Because a watcher's listener can change the model, the $digest() keeps calling the watchers until no more listeners are firing. This means that it is possible to get into an infinite loop. This function will throw 'Maximum iteration limit exceeded.' if the number of iterations exceeds 10.

The problem is not about the code here, it should be working perfectly. See the working example here: http://jsfiddle.net/fmjf8/2/

The problem is either you may have too many elements in your ngRepeating array OR the template your array, or the template you are including is doing too many stuff, raising angular's infinite loop protection. I vote for the second

于 2012-09-11T04:30:57.797 回答
2

我知道这有点不相关,但我一直在寻找类似的问题,我的任务在某个时候把我带到了这里。而且由于互联网上没有太多关于 Angular 的资源,所以我在这里为其他不幸的人写这篇文章。

我对 ng-include 的问题:

我从路由参数中加载了 ng-view 的模板(html 文件)视图,并且 ng-include 在其中一个模板中引用了其中一个模板。它在角度侧产生完全相同的响应。

据我了解,会发生这种情况:

当 ng-include 尝试将模板 html 文件嵌入到原始文件中时,它会尝试从头开始,这意味着重新加载所有内容,包括原始 html 文件、共享文件、模板所有内容;

templateX.html-> ng-include (templateY) -> templateX.html -> templateY .... 所以实际的无限循环,digest 只会防止您的浏览器由于过多的 DOM 而崩溃。

然后我尝试使用完整路径名而不是任何相对路径,它解决了我的问题。

也许您可以尝试 ng-include 中的完整路径名来解决您的问题。

希望这有所帮助。

于 2012-11-15T17:57:12.287 回答