我對AngularJS比較陌生,而且我收到了一個我不完全明白的錯誤。在這段代碼中,我得到一個類型錯誤(下面列出)。這個錯誤指向了與用戶數組sprint.users一致的行。在angular.forEach上獲取TypeError
$scope.countThisUserSprintsCompleted = function (user) {
var thisUserTotalSprintsCompleted = 0;
angular.forEach($scope.sprints, function(sprint) {
angular.forEach(sprint.users, function (sprintUser, index) {
if(sprintUser.username == user.username && sprint.status == 'complete') {
thisUserTotalSprintsCompleted++;
}
});
});
return thisUserTotalSprintsCompleted;
};
angular.js:13642 TypeError: Cannot read property 'users' of undefined
at userController.js:2123
at Object.q [as forEach] (angular.js:336)
at m.$scope.countThisUserSprintsCompleted (userController.js:2122)
at fn (eval at compile (angular.js:14539), <anonymous>:4:357)
at m.$digest (angular.js:17211)
at angular.js:17417
at e (angular.js:5912)
at angular.js:6191
這意味着,在提供的代碼中,「sprint」是未定義的。 –
沒有那麼快@guys起來,看看上面的行 – Damiano
@Damiano你可以把任何你想要的作爲參數,並不意味着他們將被定義時調用。 –