取消注释时,AngularJS函数未定义

时间:2013-08-28 22:00:52

标签: function angularjs scope promise method-missing

我有一些奇怪的问题与范围内方法的定义有关。一旦它被定义,当我想调用它(只需将其输入代码)它就会消失。我有这部分代码

if (ConfigurationService.isConfigurationChanged() === true) {
ConfigurationService.getConfiguration().then(function(data) {
    $scope.configuration = data.configuration;
    $scope.lunchers = data.lunchers;

    $scope.configuration.vegies.all_in_one_group = data.configuration.vegies.all_in_one_group;

    var lunchersLength = data.lunchers.all.length;

    for (var i = 0; i < lunchersLength; i++) {
        $scope.selectedVegies[i] = $scope.isVegie(data.lunchers.all[i]);
        $scope.selectedSpecialGroup[i] = $scope.isInSpecialGroup(data.lunchers.all[i]);
        $scope.isAbsent[i] = $scope.isAbsent(data.lunchers.all[i]);
    }   
}); 

} else if (ConfigurationService.isConfigurationChanged() === false)  {

var cached = ConfigurationService.getCachedConfiguration();
console.log($scope.isVegie);
$scope.configuration = cached.configuration;
$scope.lunchers = cached.lunchers;

$scope.configuration.vegies.all_in_one_group = cached.configuration.vegies.all_in_one_group;

var lunchersLength = cached.lunchers.all.length;

for (var j = 0; j < lunchersLength; j++) {
    $scope.selectedVegies[i] = $scope.isVegie(cached.lunchers.all[j]);
    //$scope.selectedSpecialGroup[i] = $scope.isInSpecialGroup(cached.lunchers.all[i]);
    //$scope.isAbsent[i] = $scope.isAbsent(cached.lunchers.all[i]);
}
}

现在发生了一些奇怪的事情。你能看到:

console.log($scope.isVegie);

当我有:

$scope.selectedVegies[i] = $scope.isVegie(cached.lunchers.all[j]);

评论我可以在firebug中看到方法,当我取消注释它时,它会被取消定义...

有人有类似的问题吗?

注意:

ConfigurationService.getConfiguration()

这是方法:

            getConfiguration : function() {
            var deferred = $q.defer();
            $http({
                    method :'GET',
                    url : "cgi-bin/get_configuration.py"
                })
                .success(function(data, status, headers, config) {
                    if (status === 200) {
                        angular.copy(data, configStatus.currentConfig);// = data;
                        configStatus.configurationChanged = false;
                        deferred.resolve(data);
                    }
                })
                .error(function(data, status, headers, config) {
                    console.log(data);
                    console.log(status);
                    deferred.reject(data);
                });
            return deferred.promise;
        },

1 个答案:

答案 0 :(得分:0)

发现了这个问题。事情是那个

$scope.isVegie

是在逻辑调用之后定义的。但是,当我没有其他部分时,我没有注意到。当它没有if-else时,它工作正常,即使方法被定义在“调用它的逻辑”之下。

这解决了这个问题。

相关问题