我有一个函数,用于调用 $http,执行一些代码,然后返回成功或拒绝 promise 。
function getActions() {
var self = this;
var defer = this.$q.defer();
this.$http({
url: '/api/Action/GetActions',
method: "GET"
})
.success(function (data) {
// Other code here for success
self.Actions = data;
return defer.resolve();
})
return defer.promise;
};
我想通过执行以下操作来简化此操作:
return this.$http({
url: '/api/Action/GetActions',
method: "GET"
})... etc
但是如果我这样做,那么我将无法成功获得任何代码。
谁能告诉我是否有任何方法可以简化代码?
最佳答案
function getActions()
{
var self = this;
var promise = this.$http({
url: '/api/Action/GetActions',
method: "GET"
});
promise.success(function (data) {
// Other code here for success
self.Actions = data;
});
return promise;
}
关于angularjs - 是否有更紧凑的方法在函数内使用 AngularJS 返回延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26614495/