我有一个下拉菜单,当我选择“全部”选项时,它会在控制台上显示此错误:
TypeError:无法读取未定义的属性“0” at n。$ scope.onSearchByChanged(http://localhost:8080/js/jenkinsVersion/directives/assignment-filter.js:70:81)
所以,我去了我的剧本,功能,第70行,第81个字符:
Action Menu
这是loadEditorGroups函数:
$scope.onSearchByChanged = function () {
if ($scope.filter.list.searchBy == 'DEPARTMENT_CODE' && !$scope.filterScope.departments) {
$scope.loadDepartments();
} else if ($scope.filter.list.searchBy != 'DEPARTMENT_CODE') {
$scope.filter.list.departmentId = 0;
}
if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
} else if ($scope.filter.list.searchBy != 'GROUP') {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; //line 70
}
$scope.clearFilter('text');
};
if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
}
if ($scope.filter.list.searchBy == 'DEPARTMENT_CODE' && !$scope.filterScope.departments) {
$scope.loadDepartments();
}
$scope.isStatusSelected = function (status) {
return _.indexOf($scope.filter.list.talentAssignmentStatuses, status) > -1;
};
$scope.selectTalentAssignmentStatus = function (status) {
$scope.clearFilter('text');
if ($scope.isStatusSelected(status)) {
_.remove($scope.filter.list.talentAssignmentStatuses, function (el) {
return status == el;
});
} else {
$scope.filter.list.talentAssignmentStatuses.push(status);
}
};
我还在学习JS。为什么抛出这个错误?当我从该editorGroups列表中更改我想要检索的项的值时,它只给出了相同的错误,但是带有相应的数字。如果我能提供更多信息,请与我们联系。谢谢!
答案 0 :(得分:1)
此if-else
块的逻辑可能错误:
if ($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
} else if ($scope.filter.list.searchBy != 'GROUP') {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id; //line 70
}
当两者:
时,程序将进入else if
阻止
if
条件为false
,即:($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) == false
else if
条件为true
,即:($scope.filter.list.searchBy != 'GROUP') == true
我们可以采用这两个陈述并简化它们:
!($scope.filter.list.searchBy == 'GROUP' && !$scope.filterScope.editorGroups) && ($scope.filter.list.searchBy != 'GROUP')
($scope.filter.list.searchBy != 'GROUP' || $scope.filterScope.editorGroups) && ($scope.filter.list.searchBy != 'GROUP')
($scope.filter.list.searchBy != 'GROUP')
在第2步中,我应用De Morgan's law将!(A && B)
简化为(!A || !B)
在第3步中,我简化了&&
,因为(A || B) && A
与A
相同。
实际上,我们在进入else if
块时知道的是searchBy != 'GROUP'
。我们不知道关于editorGroups
的任何,事实上,它可能是undefined
!
您可能寻找的是:
if ($scope.filter.list.searchBy == 'GROUP' || !$scope.filterScope.editorGroups) {
$scope.loadEditorGroups();
} else if ($scope.filter.list.searchBy != 'GROUP') {
$scope.filter.list.groupId = $scope.filterScope.editorGroups[0].id;
}
请注意||
条件中的if
。这样可确保else if
仅在($scope.filter.list.searchBy != 'GROUP' && $scope.filterScope.editorGroups)
时执行,以便editorGroups[0]
不会出错。如果这是你想要这个代码的话,我不太了解,所以当我错了时请纠正我。 : - )
答案 1 :(得分:0)
70:81
表示第70行,第81个字符。
问题出在editorGroups[0]
:如果editorGroups
为undefined
,则无法读取editorGroups[0]
,因为undefined
没有名为0
的属性。
确保editorGroups
未定义且您没有问题!