我正在制作一个网站,你可以在上面搜索食物,并查看它是水果、蔬菜还是其他(因为我很无聊)。虽然我对Angular不太熟悉,但我决定使用它。
然后我开始遇到这个错误:$rootScope:infdig Infinite $digest Loop。
由于页面卡顿,我无法打开Javascript控制台,所以这可能并不是错误的精确措辞。
这是我的结果视图控制器:
app.controller('resultController', ['$scope', '$routeParams', '$http',
function($scope, $routeParams, $http) {
$scope.result = $routeParams.query;
$scope.whatIsIt = function() {
$http.get('json/foods.json').success(function(data) {
var lists = JSON.parse(data);
var itIsA = 'uuggghhhhh';
if (lists['fruit'].contains($scope.result)) {
itIsA = 'fruit';
} else if (lists['vegetable'].contains($scope.result)) {
itIsA = 'vegetable';
}
});
}
}]);
这是我的应用程序模块:
var app = angular.module('fruitOrNot', [
'ngRoute'
]);
app.config(['$routeProvider' ,
function($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'layouts/fruit-search.html',
controller: 'searchController'
}).when('/:query', {
templateUrl: 'layouts/result.html',
controller: 'resultController'
});
}]);
这里是layouts/result.html:
<h1>{{ result }}</h1>
<p>{{ result }} is a {{ whatIsIt() }}</p>
我在想是什么导致了那个 $digest
循环错误,或者如何找出原因,因为我无法使用控制台。谢谢!
整个项目也可以在 Github 上找到:https://github.com/INOVA-Technology/isitafruitoravegetable.com