简体   繁体   English

在AngularJS中使用Promises在两个不同的控制器中进行链接

[英]Using Promises in AngularJS for chaining in two different controllers

I have a provider for my REST-Services named MyRestServices : 我有一个名为MyRestServices REST服务provider

app.provider('MyRestServices', function() {
  this.baseUrl = null;
  this.setBaseUrl = function(_baseUrl) {
    this.baseUrl = _baseUrl;
  };

  this.$get = ['$http', function($http) {
    var _baseUrl = this.baseUrl;

    function getMyData() {
      return $http.get(_baseUrl + 'data1/?token=' + token + '&key=' + key);
    }

    function preGetTokenAndKey() {
      return $http.get(_baseUrl + 'keyAndToken/');
    }

    return {
      getMyData: getMyData,
      preGetTokenAndKey: preGetTokenAndKey
    };
  }];
});

I configure it before calling the first REST service. 我在调用第一个REST服务之前对其进行配置。

app.config(function(MyRestServicesProvider) {
  MyRestServicesProvider.setBaseUrl('https://www.test.com/rest/');
});

And then I have a HeadCtrl controller which should call preGetTokenAndKey to get key and token which is needed for some other REST calls like getMyData . 然后我有一个HeadCtrl控制器,该控制器应调用preGetTokenAndKey来获取其他一些REST调用(如getMyData )所需的keytoken

app.controller('HeadCtrl', function (MyRestServices) {
  MyRestServices.preGetTokenAndKey().success(function(data) {
    var key = data.dataSection.key;
    var token = data.dataSection.token;
  });
});

My problem is I want to call getMyData from another controller, but I need key and token to make this call. 我的问题是我想从另一个控制器调用getMyData ,但是我需要keytoken才能进行此调用。 So I need to wait until preGetTokenAndKey was successful and I have to provide the two values to the MyRestServices provider. 因此,我需要等到preGetTokenAndKey成功完成之后,才preGetTokenAndKey两个值提供给MyRestServices提供程序。 How can I solve these problems? 我该如何解决这些问题?

It sounds like a better solution would be to chain them within your service itself. 听起来更好的解决方案是将它们链接到服务本身中。 You'd setup your own promise within preGetTokenAndKey, which gets resolved by the $http call. 您可以在preGetTokenAndKey中设置自己的承诺,该承诺可以通过$ http调用来解决。 Subsequent calls to preGetTokenAndKey() would just return the already resolved data w/o making additional $http calls. 随后对preGetTokenAndKey()的调用只会返回已解析的数据,而不会进行其他$ http调用。

So something along the lines of the following should get you started: 因此,应遵循以下步骤开始工作:

app.provider('MyRestServices', function() {
  this.baseUrl = null;
  this.setBaseUrl = function(_baseUrl) {
      this.baseUrl = _baseUrl;
  };

  this.$get = ['$http', function($http) {
    var _baseUrl = this.baseUrl;
    var _tokenAndKey = {};

    function getMyData() {
      return preGetTokenAndKey().then(function (data) {
            return $http.get(_baseUrl + 'data1/?token=' + data.dataSection.token + '&key=' + data.dataSection.key);
      });
    }

    function preGetTokenAndKey() {
      if(!_tokenAndKey.set) {
           _tokenAndKey.deferred = $http.get(_baseUrl + 'keyAndToken/').then(function(data) {
                _tokenAndKey.set = true;
                return data;
           });
      }
      return _tokenAndKey.deferred.promise;
    }

    return {
      getMyData: getMyData,
      preGetTokenAndKey: preGetTokenAndKey
    };
  }];
});

My problem is I want to call getMyData from another controller, 我的问题是我想从另一个控制器调用getMyData,

If so, you can use $broadcast to notify other controller that async call resolved and you have key/token 如果是这样,您可以使用$broadcast通知其他控制器异步呼叫已解决并且您有密钥/令牌

app.controller('HeadCtrl', function($rootScope, MyRestServices) {
    MyRestServices.preGetTokenAndKey().success(function(data) {
        var key = data.dataSection.key;
        var token = data.dataSection.token;

        $rootScope.$broadcast("getMyDataTrigger", {key: key,token: token}); 
    });
});

In other controller implement listener: 在其他控制器中实现侦听器:

$rootScope.$on("getMyDataTrigger", function(event, data){

        if(data){
           MyRestServices.getMyData(data.key, data.token);
           // ...
        }

    });

Just override getMyData : 只需覆盖getMyData

function getMyData(key, token) {
  return $http.get(_baseUrl + 'data1/?token=' + token + '&key=' + key);
}

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

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