Angularjs 那么如何使用角JS函数呢?
在将Then与函数一起使用后,我遇到了一个错误“TypeError:无法读取未定义的属性'Then'”您不能使用该函数,而是Angularjs 那么如何使用角JS函数呢?,angularjs,Angularjs,在将Then与函数一起使用后,我遇到了一个错误“TypeError:无法读取未定义的属性'Then'”您不能使用该函数,而是 app.controller('controller_name', function ($scope, $location, $auth, toastr, $http, CONFIG, $stateParams, $uibModal, $q, localStorageService, $anchorScroll, roundProgressService, $sce, c
app.controller('controller_name', function ($scope, $location, $auth, toastr, $http, CONFIG, $stateParams, $uibModal, $q, localStorageService, $anchorScroll, roundProgressService, $sce, calendarConfig, moment, $window, $rootScope) {
$scope.a1(a, b, c, d, e).then(function(data){
if (data.status == "OK") {
angular.extend($scope.pets_list, response.data['pets_list']);
}
});
$scope.a1 = function(a, b, c, d, e){
$http({
method: 'POST',
url: 'url',
data: {a: a, a: b},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (response) {
return response.data;
});
};
});
你应该看看你的控制台。Javascript使用提升,因此您可以在调用函数后定义它们 吊装示例
函数foo(){
myFunc();
函数myFunc(){
console.log('foo');
}
}
foo()如果他只是重构,那么他的例子就完美了。查看我的答案以了解详细信息。仍然有一个错误,无法读取b$scope处未定义的属性“then”。A1您是否如第2行所示声明了它?因为它是在那里声明的,所以不可能是未定义的。
scope.get = function(a, b, c, d, e) {
var defered = $q.defer();
$http.post('some_url', {a: a, a: b}).success(function(response){
defered.resolve(response);
}).error(function(response){
defered.reject(response);
});
return defered.promise;
};
};
var promise = scope.get(a, b, c, d, e);
promise.then(function(response){
console.log(response);
},function(error){
console.log(error);
});