多的$ HTTP使用厂家的承诺,角厂家、HTTP

2023-09-13 04:02:34 作者:ζ   懂我再爱我°

我试图调用一个工厂,我使用冲洗出来的数据和设置默认选项多输入多$ HTTP请求。我想,然后调用一个承诺,一旦这些所有3均由内部完成,真正的表单数据调用(如果有的话,有时也不会在这种情况下,它不会做任何事情)覆盖到位的默认值。

I am attempting to call multiple $http request in a factory which I Am using to flush out multiple inputs with data and set default selections. I Want to then call a promise once all 3 of these are done to call in the real form data (if there is any, sometimes there will not be in which case it will do nothing) to overwrite the defaults in place.

因此​​,这里是我的尝试这个 -

So here is my attempt at this -

工厂我成立了一个工厂调用所有3个输入和它们的默认值(我被单独发送它们,我不能现在更改)。它看起来是这样的:

the factory I set up a factory to call all 3 inputs and their defaults (I am being sent them individually, i cannot change this for now). It looks like this:

.factory("getDefaults", function() {

return {
    instructions: function() {
        $http({
        method: "GET",
        url: "/getStringDropdown/materials"
    })
    .success(function(data, status, headers, config){
        $scope.instructions.materials = data.text;
    });

    $http({
        method: "GET",
        url: "/getStringDropdown/reinforce"
    })
    .success(function(data, status, headers, config){
        $scope.reinforce = data.options;
        $scope.instructions.reinforce = $scope.reinforce[data.default];
    });

    $http({
        method: "GET",
        url: "/getStringDropdown/procedure"
    })
    .success(function(data, status, headers, config){
        $scope.procedures = data.options;
        $scope.instructions.procedures = $scope.procedures[data.default];
    });
     //return data here? 

    }
  };

})

在这里我的问题是 - 我一定要回到这里的数据?而且还可以在这里我定义范围正在使用的(在实际控制结合在)。我是pretty知道这是错的,但我不能'找到如何构建这样的一个很好的例子正常。

My question here is - do i have to return the data here? And also can I define the scopes here that are being used (as apposed to in the actual controller). I'm pretty sure this is wrong but I cant' find a good example of how to structure something like this properly.

在控制器上调用

所以我控制器我的想法是那么我会尝试这样的事情 -

So i the controller my thinking is i would then try something like this -

    getDefaults.instructions().then(function(){
            //possible return data here
            //AFTER data is flushed out call farm data

    $http({
        method: "GET",
        url: "/getSavedFormData/formID"
    })
    .success(function(data, status, headers, config){
        $scope.instructions.materials= data.materials;
        $scope.instructions.procedures = $scope.procedures[data.procedure];
        $scope.instructions.reinforce = $scope.reinfoce[data.reinforcement];
    });

        });

那么大图片 - 我想获得这3个调用运行和填充,然后第二个。我不知道什么可能是也可能不是最好的方法,工厂似乎让基础上,试图将3个电话合并为1的地方有一个承诺,当他们都做的感觉。我想我需要返回的数据,但它是pretty很好,如果我可以定义范围适用于工厂控制器。我仍然得到我的轴承具有角,因此任何/所有的指导会更AP preciated。感谢您阅读!

So big picture - I am trying to get these 3 calls to run and populate and THEN the second one. I'm not sure what might or might not be the best approach, factory seemed to make sense based on the trying to consolidate the 3 calls into 1 place with a promise when they are all done. I'm thinking i need to return the data, but it would be pretty nice if i could define the scopes for the controller in the factory. I am still getting my bearing with angular so any/all guidance would be much appreciated. Thanks for reading!!

推荐答案

您服务并不知道你的$范围的开箱即用,也不你可能希望它是作为服务的全部意义在于协助您code的模块化。

Your service is not aware of your $scope out of the box, nor do you probably want it to be as the whole point of services is to assist with the modularity of your code.

什么你可能想要做的实际上是从服务返回$ HTTP的承诺,使您的.s​​uccess()回调可在$范围通过闭合实际设置模型(控制器内部存在)。

What you probably want to do is actually return the $http promise from your service so that your .success() callback can actually set models on the $scope via closure (being inside the controller).

所以,你的工厂会更喜欢这样的:

So your factory would be more like this:

.factory("getDefaults", function() {
  return {
    instructions: $http({ method: "GET", url: "/getStringDropdown/materials" })
  }
});

如果你真以为你再也不需要这些HTTP单独呼叫并只关心时,他们都决心。你可以返回,将解决$ q.all()承诺,当他们都决心:

If you really think you'll never need those http calls separately and you only care about when they all resolve. You could return a $q.all() promise that will resolve when they all resolve:

.factory("getDefaults", function($http, $q) {
  var promise1 = $http({ method: "GET", url: "/getStringDropdown/materials" });
  var promise2 = $http({ method: "GET", url: "/getStringDropdown/materials" });
  var promise3 = $http({ method: "GET", url: "/getStringDropdown/materials" });
  return {
    data: $q.all([promise1,promise2,promise3]),
    anotherCall: $http.get('/anothercallUrl')
  }
});

所以,现在从你的控制器你可以只是做:

So now from your controller you could just do:

function myCtrl($scope,getDefaults){
   getDefaults.data.then(function(data){
     //access all three of your promises, their data, and set $scope models here
     getDefaults.anotherCall.success(function(data){
       //or another http call
     });
   };
}

$ q.all文档在这里: https://docs.angularjs.org/ API / NG /服务/ $ q