我在使用ng-include时遇到以下问题。我错过了什么?有人有类似的问题吗?
test.html
。<h1>Testing</h1>
重复了很多次。 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
我正在做的是使用精简版index.html的简单测试
index.html
<html>
<head>
<link href="static/bower-components/bootstrap-css/css/bootstrap.min.css" rel="stylesheet">
<link href="static/src/css/app.css" rel="stylesheet">
<script src="static/bower-components/angular/angular.js"></script>
<script src="static/bower-components/angular-route/angular-route.js"></script>
<script src="static/bower-components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script src="static/src/js/app.js"></script>
<script src="static/src/js/services.js"></script>
<script src="static/src/js/controllers.js"></script>
<script src="static/src/js/filters.js"></script>
<script src="static/src/js/directives.js"></script>
</head>
<body ng-app="myApp">
<h1> Testing </h1>
<div ng-include="'test.html'"> </div>
</body>
</html>
的test.html
<h3> included template </h3>
在浏览器中我得到:
检查浏览器中的html,显示index.html的递归包含:
<body ng-app="myApp" class="ng-scope">
<h1> Testing </h1>
<!-- ngInclude: 'test.html' --><div ng-include="'test.html'" class="ng-scope">
<link href="static/bower-components/bootstrap-css/css/bootstrap.min.css" rel="stylesheet" class="ng-scope">
<link href="static/src/css/app.css" rel="stylesheet" class="ng-scope">
<script src="static/bower-components/angular/angular.js" class="ng-scope"></script>
<script src="static/bower-components/angular-route/angular-route.js" class="ng-scope"></script>
<script src="static/bower-components/angular-bootstrap/ui-bootstrap-tpls.js" class="ng-scope"></script>
<script src="static/src/js/app.js" class="ng-scope"></script>
<script src="static/src/js/services.js" class="ng-scope"></script>
<script src="static/src/js/controllers.js" class="ng-scope"></script>
<script src="static/src/js/filters.js" class="ng-scope"></script>
<script src="static/src/js/directives.js" class="ng-scope"></script>
<h1 class="ng-scope"> Testing </h1>
<!-- ngInclude: 'test.html' --><div ng-include="'test.html'" class="ng-scope">
<link href="static/bower-components/bootstrap-css/css/bootstrap.min.css" rel="stylesheet" class="ng-scope">
<link href="static/src/css/app.css" rel="stylesheet" class="ng-scope">
<script src="static/bower-components/angular/angular.js" class="ng-scope"></script>
<script src="static/bower-components/angular-route/angular-route.js" class="ng-scope"></script>
<script src="static/bower-components/angular-bootstrap/ui-bootstrap-tpls.js" class="ng-scope"></script>
<script src="static/src/js/app.js" class="ng-scope"></script>
<script src="static/src/js/services.js" class="ng-scope"></script>
<script src="static/src/js/controllers.js" class="ng-scope"></script>
<script src="static/src/js/filters.js" class="ng-scope"></script>
<script src="static/src/js/directives.js" class="ng-scope"></script>
<h1 class="ng-scope"> Testing </h1>
<!-- ngInclude: 'test.html' --><div ng-include="'test.html'" class="ng-scope">
<link href="static/bower-components/bootstrap-css/css/bootstrap.min.css" rel="stylesheet" class="ng-scope">
<link href="static/src/css/app.css" rel="stylesheet" class="ng-scope">
<script src="static/bower-components/angular/angular.js" class="ng-scope"></script>
<script src="static/bower-components/angular-route/angular-route.js" class="ng-scope"></script>
<script src="static/bower-components/angular-bootstrap/ui-bootstrap-tpls.js" class="ng-scope"></script>
<script src="static/src/js/app.js" class="ng-scope"></script>
<script src="static/src/js/services.js" class="ng-scope"></script>
<script src="static/src/js/controllers.js" class="ng-scope"></script>
<script src="static/src/js/filters.js" class="ng-scope"></script>
<script src="static/src/js/directives.js" class="ng-scope"></script>
<h1 class="ng-scope"> Testing </h1>
<!-- ngInclude: 'test.html' --><div ng-include="'test.html'" class="ng-scope">
<link href="static/bower-components/bootstrap-css/css/bootstrap.min.css" rel="stylesheet" class="ng-scope">
.......
...
</div>
</div>
</body>
答案 0 :(得分:2)
错误的来源是包含html模板的错误路径。如果模板路径不正确且与任何服务器路由不匹配,则服务器默认为服务整个index.html
。这会启动一个无限循环,因为在加载index.html
时,会再次使用错误的路径请求include
模板,并且会一次又一次地包含index.html,从而产生“10 $” digest()迭代达到“错误
在我的情况下,从服务器请求test.html
的正确路径是static/src/views/test.html
。
从此更改包含
<div ng-include="'test.html'"> </div>
到这个
<div ng-include="'static/src/views/test.html'"> </div>
解决了这个问题。