Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 控制台TypeError中AngularJs筛选器引发错误:无法读取未定义的属性“length”_Javascript_Angularjs_Angularjs Scope_Angularjs Ng Repeat - Fatal编程技术网

Javascript 控制台TypeError中AngularJs筛选器引发错误:无法读取未定义的属性“length”

Javascript 控制台TypeError中AngularJs筛选器引发错误:无法读取未定义的属性“length”,javascript,angularjs,angularjs-scope,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Scope,Angularjs Ng Repeat,我正在尝试在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

我正在尝试在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

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
};
});

当I console.logwebsites.length时,它给出控制台中对象的数量。同时它返回值。如何调试此错误

你能把plunker放在这里吗?我已经添加了更多的信息来让问题更清楚。我正在使用服务从django rest framework api获取数据,所以它们没有json文件。我不知道如果没有数据,我是否可以把plunker放在数组中,因为数组使用服务从api获取数据。你现在能看到代码吗?希望添加的信息让问题变得更清楚错误表示未定义网站。如果console.log起作用,那么代码中可能有2个相同的网站元素,可能是在您的工厂?是否可能是在加载对象之前尝试获取对象的长度?@Aarmora我在问题中提到过,当我使用console.logwebsites.length时,它给出了控制台中对象的数量。所以我认为加载时间没有任何问题
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
};
});