我不知道如何去回复承诺。我试图在嵌套方法返回的结果,但更愿意返回结果有两种不同的方法如图所示算账:
$scope.relatedContacts = function (accountId) {
if (!lodash.isNil(accountId)) {
try {
return restangular.one('user')
.one('contactimages')
.get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId })
.then(function (response) {
return response.data;});
}
}
宁愿解决下面的例子:
$scope.relatedContacts = function (accountId) {
if (!lodash.isNil(accountId)) {
try {
var deferred = $q.defer();
return restangular.one('user')
.one('contactimages')
.get({ 'mappedRelatedContactsPath': $scope.mappedRelatedContactsPath, "account": accountId })
return deferred.promise;
}
catch (err) {
$scope.contactsPopulated = false;
}
}
}
$scope.relatedContacts().then(function (response) {
//Some logic here
}
目前我我越来越:“类型错误:无法读取属性‘然后’未定义 的”
感谢所有
对于第一种方法,我添加$ Q在我的控制器,但现在收到错误:类型错误:$ q是不是一个函数:.controller( 'SelectUserController',[ '$范围', 'PubSub的',“Pnotify ','Common.InfiniteListService','SelectUserService','Restangular','$ q', 函数($ scope,pubSub,pnotify,InfiniteListService,selectUserService,restangular,$ q){ –
问题是我想要执行一些逻辑与响应:例如;当承诺被解决时,我想检查额外的逻辑。 –
isNil将工作,因为我只有在帐户不为空时调用方法。 –