Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS如何仅在解决承诺后执行代码?(带Restanglar)_Angularjs_Promise_Restangular - Fatal编程技术网

AngularJS如何仅在解决承诺后执行代码?(带Restanglar)

AngularJS如何仅在解决承诺后执行代码?(带Restanglar),angularjs,promise,restangular,Angularjs,Promise,Restangular,这可能是一个不着边际的问题,但我仍然无法理解承诺,尤其是如何使用承诺编写代码。(我读过几篇文章,但大部分都是抽象的,我只是写得不够清楚) 我有一个AngujlarJS应用程序,它通过http请求向另一台服务器获取数据,该服务器首先发送一个承诺。我已经能够从promise中检索响应并在我的应用程序中使用它。但是因为我的代码写得很差。它在承诺被解决之前执行其他代码,从而导致问题。它在拥有数据之前开始加载页面 我得到的是: var userTotals = *http request which re

这可能是一个不着边际的问题,但我仍然无法理解承诺,尤其是如何使用承诺编写代码。(我读过几篇文章,但大部分都是抽象的,我只是写得不够清楚) 我有一个AngujlarJS应用程序,它通过http请求向另一台服务器获取数据,该服务器首先发送一个承诺。我已经能够从promise中检索响应并在我的应用程序中使用它。但是因为我的代码写得很差。它在承诺被解决之前执行其他代码,从而导致问题。它在拥有数据之前开始加载页面

我得到的是:

var userTotals = *http request which returns a promise

$scope.data = userTotals.$object

//code that does someting with $scope.data
我需要的是(我想)


然而,我不能得到正确的语法

这就是它通常的样子:

var promise = $http.post('/url');

console.log('Request started');

promise.then(function(result) {
  console.log('Success');
  console.log(result);
}, function() {
  console.log('Failure');
});
事实上,这帮了我很大的忙,让我了解了承诺的概念


希望这有帮助

假设您使用的是引导模式,您可以执行以下操作:

var successCallback = function(){//...};
var errorCallback = function(){//...};

$http
 .post('url', data)
 .success(successCallback)
 .error(errorCallback);

//OR

$http
 .post('url', data)
 .then(successCallback, errorCallback);
function openModalWithProgressIndicator(deferred) {
  const progressModal = $uibModal.open({
    templateUrl: 'templates/modals/progress.html',
    backdrop: 'static'
  });
  return deferred.then(function (value) {
    return value;
  }).finally(function () {
    progressModal.close();
  });
}
const deferred = $http.post('http://somewhere/data', {foo: 'bar'});

openModalWithProgressIndicator(deferred)
  .then(function (httpResponse) {
    // do sth with httpResponse.data
  }).catch(function (error) {
    // do sth with error
  });
传递给此函数的
deferred
参数是一个承诺。也就是说,您现在可以执行以下操作:

function openModalWithProgressIndicator(deferred) {
  const progressModal = $uibModal.open({
    templateUrl: 'templates/modals/progress.html',
    backdrop: 'static'
  });
  return deferred.then(function (value) {
    return value;
  }).finally(function () {
    progressModal.close();
  });
}
const deferred = $http.post('http://somewhere/data', {foo: 'bar'});

openModalWithProgressIndicator(deferred)
  .then(function (httpResponse) {
    // do sth with httpResponse.data
  }).catch(function (error) {
    // do sth with error
  });

因此,需要注意的主要一点是始终执行的
最终
回调。

http请求返回一个
承诺
,您有
然后
谢谢,这正是我需要的。我也会查文件的。