简体   繁体   English

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

[英]AngularJS how do I execute code only after a promise is resolved? (with Restangular)

This might be a nooby question but I still haven't been able to get my head around promises and specifically how to write code with them. 这可能是一个nooby问题,但我仍然无法绕开promise,尤其是如何用promise编写代码。 (I've read several articles but most of them are abstract and I simply haven't written enough to have a clear picture) I've got an AngujlarJS application that gets data through a http request to another server which sends a promise at first. (我读过几篇文章,但其中大多数都是抽象的,而我写的不够清晰)。我有一个AngujlarJS应用程序,该应用程序通过http请求将数据获取到另一台服务器,该服务器首先发送一个Promise 。 I've been able to retrieve the response from the promise and use it in my app. 我已经能够从诺言中检索响应并在我的应用程序中使用它。 However because my code is poorly written. 但是,因为我的代码写得不好。 It executes other code before the promise is resolved leading to problems. 在兑现承诺导致问题之前,它会执行其他代码。 It starts loading the page before it has the data. 它开始加载具有数据的页面。

what i have is: 我所拥有的是:

var userTotals = *http request which returns a promise

$scope.data = userTotals.$object

//code that does someting with $scope.data

What i need is (I think) 我需要的是(我认为)

var userTotals = *http request which returns a promise

$scope.data = userTotals.$object.
  beforethisresolves(function{ 
     show fancy loading icon or something })
  .whenthis resolves(function{
    //code that does someting with $scope.data
  }

however I can't get the syntax correct. 但是我不能得到正确的语法。

This is what it looks like in general: 大致如下所示:

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

console.log('Request started');

promise.then(function(result) {
  console.log('Success');
  console.log(result);
}, function() {
  console.log('Failure');
});

In fact, $q AngularJS documentation helped me a good deal to get my head around promises concept. 实际上, $ q AngularJS文档对我帮助了很多人,使我可以绕开Promise概念。

Hope this helps! 希望这可以帮助!

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

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

//OR

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

Assuming that you're using Bootstrap modal you can do the following: 假设您正在使用Bootstrap模式,则可以执行以下操作:

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();
  });
}

The deferred argument passed to this function is a promise. 传递给此函数的deferred参数是一个承诺。 That said you can now do the following: 也就是说,您现在可以执行以下操作:

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
  });

So the main point to note is the finally callback that's always executed. 因此,要注意的重点是始终执行的finally回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM