错误:angularJS上的XXX为null

时间:2014-07-17 09:12:04

标签: javascript angularjs

我遇到了这个错误:

ERROR : DISCIPLINE.EVENTS is NULL

由第二个if声明提出。

以下是代码:

angular.forEach($scope.form.disciplines, function(discipline) {
        if (angular.isUndefined(discipline.events) || discipline.events == null){ 
            angular.forEach(discipline.compets, function(compet) { 
                var key = compet.eventCode;
                if (angular.isUndefined(discipline.events[key]) || discipline.events[key] == null) {
                    discipline.events[key] = {fhi:{presented : null, box : null, notAccepted : null, withdrawn : null, accepted : null}}                         }
            });                              
        }   
    }); 

感谢所有人。

2 个答案:

答案 0 :(得分:0)

错误在这里,discipline.events不是数组,等于null:

discipline.events[key]

在获取数组值之前,请测试事件是否为null或是否为数组。像这样:

if (!discipline.events || angular.isUndefined(discipline.events[key]) || discipline.events[key] == null) {

或者也许:

if (typeof(discipline.events) == "object" || /*...*/) {

答案 1 :(得分:0)

只需将discipline.events初始化为内部forEach之前的空数组:

if (...) {
    discipline.events = [];
    angular.forEach(
        ...
    );                              
}