我正在尝试在angularjs中使用自定义过滤器。过滤器正在运行,但它在控制台中抛出错误。
控制台:
TypeError: Cannot read property 'length' of undefined
at Scope.<anonymous> (http://127.0.0.1:8080/static/restapi/js/refine.js:216:20)
at fnInvoke (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:10033:21)
at OPERATORS.| (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:9548:59)
at extend.constant (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:9978:14)
at Object.$watchCollectionWatch (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:11658:22)
at Scope.$digest (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:11822:40)
at Scope.$apply (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:12083:24)
at done (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:7841:45)
at completeRequest (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:8042:7)
at XMLHttpRequest.xhr.onreadystatechange (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:7985:11) angular.js:9435(anonymous function) angular.js:9435
TypeError: Cannot read property 'length' of undefined
at Scope.<anonymous> (http://127.0.0.1:8080/static/restapi/js/refine.js:216:20)
at fnInvoke (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:10033:21)
at OPERATORS.| (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:9548:59)
at extend.constant (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:9978:14)
at Object.$watchCollectionWatch (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:11658:22)
at Scope.$digest (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:11822:40)
at Scope.$apply (http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:12083:24)
at c (http://127.0.0.1:8080/static/restapi/js/ng-infinite-scroll.min.js:2:595)
at http://127.0.0.1:8080/static/restapi/js/ng-infinite-scroll.min.js:2:801
at http://127.0.0.1:8080/static/restapi/lib/angular/angular.js:13609:28 angular.js:9435
这是代码:
HTML
<div ng-controller="MyCtrl">
<center>
<form>
<input ng-model="searchString" type="text">
<button ng-click="go('/search/'+searchString)">Search</button>
</form>
<h1>Results</h1>
<div ng-controller='DemoController'>
<ul>
<li ng-repeat="i in websites | searchFor:searchString">
<a href="/website/{{i.domain_name}}" ><h3>{{i.domain_name}}</h3></a>
</li>
</ul>
</div>
</center>
</div>
app.js
myApp.controller('MyCtrl', ['$scope', '$routeParams','$location','$filter',
function($scope, $routeParams,$location,$filter) {
$scope.searchString=$routeParams.q;
$scope.go = function (path) {
$location.path(path);
};
}]);
//In websitesSvc I use angular services to get data using $http
myApp.controller('DemoController', ['$scope','websitesSvc',
function($scope,websitesSvc) {
websitesSvc.getwebsites().then(function(websites){
$scope.websites = websites;
})}
]);
myApp.filter('searchFor', function(){
return function(websites, searchString){
if(!searchString){
return 0;
}
var result = [];
searchString = searchString.toLowerCase();
console.log(websites.length); //showing the array length here in console
var len = websites.length; //Error in console in this line
var i =0;
for(i=0;i<len;i++){
if(websites[i].domain_name.toLowerCase().indexOf(searchString) !== -1){
result.push(websites[i]);
}
};
return result; //Results are shown on HTML page it works
};
});
当我console.log(websites.length)
时,它给出了否。控制台中的对象。还返回值。如何调试此错误?