0

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

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]);

评论我可以在萤火虫中看到方法,当我取消注释它时,它变得未定义......

有人有类似的问题吗?

笔记:

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;
        },
4

1 回答 1

0

发现问题。事情是这样的

$scope.isVegie

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

这解决了这个问题。

于 2013-09-17T07:23:17.567 回答