这是我的代码。我收到一条错误消息:TypeError: undefined is not a function
:
我认为问题在于我对承诺缺乏了解,但如果有人能够启发我,我会非常感激。
getProgramDetails: function (program) {
var _this = this;
this.getDetails(program).then(function() {
....
});
},
getDetails: function (program) {
var _this = this;
var deferred = $q.defer();
// Error occurs at this line
this.http.get({id: program.programID}).then(function(results) {
if (results && results.programID) {
_this.isNewProgram = false;
_this.selectedProgram = {
...
};
} else {
_this.isNewProgram = true;
_this.selectedProgram = {
...
};
}
deferred.resolve();
});
return deferred.promise;
},
http: $resource(
$window.detailsEndpoint',
{ id: '@id' },
{ //parameters default
update: {
method: 'PUT',
params: {}
},
get: {
method: 'GET',
params: {
id: '@id'
}
},
post: {
method: 'POST'
}
})
答案 0 :(得分:1)
我认为$resource
没有像.then()
这样的$http
方法,而是尝试将回调作为第二个参数传递
this.http.get({id: program.programID}, function(results){});
或致电其$promise
this.http.get({id: program.programID}).$promise.then(function(results){});