无法读取未定义的属性“长度” - AngularJS 1.6

时间:2017-08-14 03:32:07

标签: javascript angularjs

我在控制器中找不到以前正常工作的错误。 错误消息引用第23行。( TypeError:无法读取未定义的属性'length'

$scope.filteredItems = $scope.list.length;

这是一个完整的脚本

var appMembers = angular.module('myAppMembers', ['ui.bootstrap']);

appMembers.filter('startFrom', function() {
return function(input, start) {
    if(input) {
        start = +start; //parse to int
        return input.slice(start);
    };
    return [];
}
});
appMembers.controller('membersCrtl', function ($scope, $http, $timeout) {
$http({method: "GET", url: "../angularJS/ajax/getMembers.php"})
.then(function onSuccess(response) {
    var members = response.members;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    $scope.list = members;
    $scope.currentPage = 1; //current page
    $scope.entryLimit = 50; //max no of items to display in a page
    $scope.filteredItems = $scope.list.length; //Initially for no filter  
    $scope.totalItems = $scope.list.length;
})
.catch(function onError(response) {
    var members = response.members;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    console.log(response);
});
$scope.setPage = function(pageNo) {
    $scope.currentPage = pageNo;
};
$scope.filter = function() {
    $timeout(function() { 
    $scope.filteredItems = $scope.filtered.length;
    }, 10);
};
$scope.sort_by = function(predicate) {
    $scope.predicate = predicate;
    $scope.reverse = !$scope.reverse;
};
});

文件路径和名称(appMembers,myAppMembers)是正确的。

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试将$scope.list.length替换为members.length。要确保members确实是一个数组,请尝试通过将response值输出到json来检查它。

console.log("response : " + JSON.stringify(response));