Angularjs函数从另一个函数调用并等待其响应

时间:2022-09-29 16:56:01

I've search for a sollution but I didn't find something like that.

我正在寻找一种溶剂,但我没有找到类似的东西。

I'm using angular, I want to call a function inside another function, and wait for its response.

我正在使用angular,我想在另一个函数中调用一个函数,并等待它的响应。

the 2nd function is:

第二个功能是:

self.changeProvider = function() {

    var contexec = false;

    if (!checkIsFit()) {
      contexec = true;
    } else {
      contexec = false;
    }


  if (contexec) {
      var modalOptions = {
          closeButtonText: $translate.instant('closeButtonText'),
          actionButtonText: $translate.instant('ok'),
          headerText: $translate.instant('changeProvidertitle'),
          bodyTemplate: '../themes/default/src/app/shoppingCart/changeProvider/changeProvider.tpl.html',
          margin: true
      };


      var modalDefaults = {
          backdrop: 'static',
          templateUrl: '../themes/default/src/app/shoppingCart/changeProvider/changeProvider.tpl.html',
          controller: 'ChangeProviderCtrl',
          size: 'sm',
          resolve: {
              modalData: function() {
                  return {
                      data: $scope.arrayToChangeProvider
                  };
              }
          }
      };

      modalService.showModal(modalDefaults, modalOptions)
          .then(function(result) {
//some stuff
              });
          }
      };

And the other function:

而另一个功能:

  var checkIsFit = function() {

    if ( $scope.cabstatus != 4 ) {
      return false;
    } else {

            var modalOptions = {
            closeButtonText: $translate.instant('closeButtonText'),
            actionButtonText: $translate.instant('ok'),
            headerText: $translate.instant('cabisfittedtitle'),
            bodyTemplate: '../themes/default/src/app/shoppingCart/checkIsFit/checkIsFit.tpl.html',
            margin: true
           };


        var modalDefaults = {
            backdrop: 'static',
            templateUrl: '../themes/default/src/app/shoppingCart/checkIsFit/checkIsFit.tpl.html',
            controller: 'CheckIsFitCtrl',
            size: 'sm',
            resolve: {
                modalData: function() {
                    return {
                    };
                }
            }
        };

          modalService.showModal(modalDefaults, modalOptions)
            .then(function(result) {
              if (result.msg === 'ok') {

                var params = {
                  token: $scope.token,
                  fkidpedido: $scope.pendingOrderLineList[0].FK_IDPEDIDO,
                  userid : $scope.userid
                  };

                shoppingCartService.postResetAgr(params, function() {
                    return true;
                }, function() {
                    /*Notification.error({
                        message: $translate.instant('components.activity.actions.deleteActivityError')
                    });*/
                });
                return false;
              } else {
                return true;
              }
          });
      }
  };

The problem is the function changeProvider still executing and opens the modal first to resolve the funcion checkIsFit() I want to wait checkIsFit is resolved and then continue with the functions of changeProvider I cannot include the checkIsFit() functionallity inside changeProvider because I want to use checkIsFit() into another functions.

问题是函数changeProvider仍然执行并打开模态首先解析函数checkIsFit()我想等待checkIsFit被解析然后继续使用changeProvider的函数我不能在changeProvider中包含checkIsFit()函数,因为我想使用checkIsFit()进入另一个函数。

Any help will be appreciate.

任何帮助将不胜感激。

Thanks in advance

提前致谢

2 个解决方案

#1


4  

I believe what you're looking for are deferred objects and promises. Check out the documentation for $q:

我相信你所寻找的是延期对象和承诺。查看$ q的文档:

https://docs.angularjs.org/api/ng/service/$q

https://docs.angularjs.org/api/ng/service/$q

I'd recommend giving this a good read because this is a really important and powerful concept for ANY Javascript developer.

我建议给它一个很好的阅读,因为对于任何Javascript开发人员来说这是一个非常重要和强大的概念。

At the essence, deferred objects and promises allow you run asynchronous processes and callback to a function when a process is complete.

从本质上讲,延迟对象和promise允许您在进程完成时运行异步进程并回调函数。

#2


0  

The modalService.showmodal method returns a promise. Create functions that return those promises.

modalService.showmodal方法返回一个promise。创建返回这些承诺的函数。

  var modalPromise1Fn = function () {
      var promise1 = 
          modalService.showModal(modalDefaults1, modalOptions2)
              .then(function(result) {
                  //some stuff
               });
      return promise1;
  };

  var modalPromise2Fn = function () {
      var promise2 = 
          modalService.showModal(modalDefaults2, modalOptions2)
              .then(function(result) {
                  //some stuff
               });
      return promise2;
  };

This use the .then method of the first promise to chain the second promise.

这使用第一个承诺的.then方法来链接第二个承诺。

var derivedPromise = 
    modalPromise1Fn().then( function() {
        var promise2 = modalPromise2Fn();
        //return to chain the second promise
        return promise2;
   });

From the Docs:

来自Docs:

Chaining promises

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises.

因为调用promise的.then方法会返回一个新的派生promise,所以很容易创建一个promise链。

It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. This makes it possible to implement powerful APIs .

可以创建任意长度的链,并且由于可以使用另一个承诺解决承诺(这将进一步推迟其解析),因此可以在链中的任何点暂停/推迟承诺的解决。这使得实现强大的API成为可能。

-- AngularJS $q Service API Reference -- Chaining Promises

- AngularJS $ q服务API参考 - 链接承诺

#1


4  

I believe what you're looking for are deferred objects and promises. Check out the documentation for $q:

我相信你所寻找的是延期对象和承诺。查看$ q的文档:

https://docs.angularjs.org/api/ng/service/$q

https://docs.angularjs.org/api/ng/service/$q

I'd recommend giving this a good read because this is a really important and powerful concept for ANY Javascript developer.

我建议给它一个很好的阅读,因为对于任何Javascript开发人员来说这是一个非常重要和强大的概念。

At the essence, deferred objects and promises allow you run asynchronous processes and callback to a function when a process is complete.

从本质上讲,延迟对象和promise允许您在进程完成时运行异步进程并回调函数。

#2


0  

The modalService.showmodal method returns a promise. Create functions that return those promises.

modalService.showmodal方法返回一个promise。创建返回这些承诺的函数。

  var modalPromise1Fn = function () {
      var promise1 = 
          modalService.showModal(modalDefaults1, modalOptions2)
              .then(function(result) {
                  //some stuff
               });
      return promise1;
  };

  var modalPromise2Fn = function () {
      var promise2 = 
          modalService.showModal(modalDefaults2, modalOptions2)
              .then(function(result) {
                  //some stuff
               });
      return promise2;
  };

This use the .then method of the first promise to chain the second promise.

这使用第一个承诺的.then方法来链接第二个承诺。

var derivedPromise = 
    modalPromise1Fn().then( function() {
        var promise2 = modalPromise2Fn();
        //return to chain the second promise
        return promise2;
   });

From the Docs:

来自Docs:

Chaining promises

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises.

因为调用promise的.then方法会返回一个新的派生promise,所以很容易创建一个promise链。

It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. This makes it possible to implement powerful APIs .

可以创建任意长度的链,并且由于可以使用另一个承诺解决承诺(这将进一步推迟其解析),因此可以在链中的任何点暂停/推迟承诺的解决。这使得实现强大的API成为可能。

-- AngularJS $q Service API Reference -- Chaining Promises

- AngularJS $ q服务API参考 - 链接承诺