Function 取消注释时AngularJS函数未定义
我有一些关于范围内方法定义的奇怪问题。一旦定义了它,当我想调用它时(只需将它输入代码),它就会消失。我有这部分代码Function 取消注释时AngularJS函数未定义,function,angularjs,scope,promise,method-missing,Function,Angularjs,Scope,Promise,Method Missing,我有一些关于范围内方法定义的奇怪问题。一旦定义了它,当我想调用它时(只需将它输入代码),它就会消失。我有这部分代码 if (ConfigurationService.isConfigurationChanged() === true) { ConfigurationService.getConfiguration().then(function(data) { $scope.configuration = data.configuration; $scope.lunchers =
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]);
}
}
当我有:
$scope.selectedVegies[i] = $scope.isVegie(cached.lunchers.all[j]);
注释:我可以在firebug中看到该方法,当我取消注释它时,它将变得未定义
有人有类似的问题吗
注:
这种方法是:
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;
},
发现了问题。问题是
$scope.isVegie
在调用它的逻辑之后定义。然而,当我没有其他部分时,我没有注意到。当它没有if-else时,即使方法是在调用它的逻辑的“下”定义的,它也可以正常工作
这解决了问题。哪里是
$scope.isVegie()调用服务的控制器中的代码>方法。如果第一个条件为真,那么它可以找到它。在其他方面,如果它不能。。。另外,奇怪的是,当我评论这一行时:$scope.selectedVegies[I]=$scope.isVegie(cached.午餐者.all[j]);在else condition console.log($scope)中,将转储正确的作用域(内有isVegie),如果我删除注释,它将不会有任何方法。。。很奇怪
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;
},
$scope.isVegie