简体   繁体   English

AngularJs-同时拨打多个服务电话?

[英]AngularJs - make several service calls simultaneously?

I have a very large search form that contains, among other fields, 8 select controls which must be populated from web services. 我有一个非常大的搜索表单,除其他字段外,其中包含8个必须从Web服务填充的选择控件。 I'm struggling a bit with how to best accomplish this using AngularJs. 我在如何最好地使用AngularJs做到这一点上有些挣扎。 Do I call one, and put the next in the 'then' clause, and then ext in that 'then' clause, and so on? 我调用一个,然后将下一个放在“ then”子句中,然后在该“ then”子句中ext,依此类推吗? Just looking at that makes me think there must be a better, well-formed method of doing this that I'm just missing because I'm new with Angular... 仅仅看一下就使我认为必须有一种更好的,格式正确的方法来实现此目的,而我只是想念它,因为我是Angular的新手...

To illustrate the issue, I have the following HTML (part of the entire form): 为了说明这个问题,我有以下HTML(整个表单的一部分):

<div class="row">
    <div class="col-md-12">
        <div class="col-md-2">
            <label for="ddlRace">Race</label>
            <select id="ddlRace" class="form-control"></select>
        </div>
        <div class="col-md-2">
            <label for="ddlHairColor">Hair Color</label>
            <select id="ddlHairColor" class="form-control">
            </select>
        </div>
        <div class="col-md-2">
            <label for="ddlEyeColor">Eye Color</label>
            <select id="ddlEyeColor" class="form-control">
            </select>
        </div>
        <div class="col-md-2">
            <input type="text" id="tbHeight" class="form-control">
        </div>
        <div class="col-md-2">
            <input type="text" id="tbWeight" class="form-control">
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-3">
        <label for="tbStreetNumber">House Number</label>
        <input type="text" id="tbStreetNumber" class="form-control">
    </div>
    <div class="col-md-2">
        <label for="ddlStreetPrefix">Prefix</label>
        <select id="ddlStreetPrefix" class="form-control">
            <option value="">Prefix</option>
        </select>
    </div>
    <div class="col-md-5">
        <label for="tbStreetName">Street Name</label>
        <input type="text" id="tbStreetName" class="form-control">
    </div>
    <div class="col-md-2">
        <label for="ddlStreetSuffix">Suffix</label>
        <select id="ddlStreetSuffix" class="form-control">
            <option value="">Suffix</option>
        </select>
    </div>
</div>

I have to populate the Race, Hair colors, Eye colors, Street prefix and Street suffix from service calls. 我必须在服务电话中填写种族,头发颜色,眼睛颜色,街道前缀和街道后缀。 I have a factory setup with calls for each of those, but I need to run all of them when the form initially loads so I can populate the fields. 我有一个工厂设置,其中每个都有调用,但是在表单初次加载时我需要运行所有这些调用,以便填充字段。 So what is the best method of doing this using the deferred/promise API? 那么,使用延迟/承诺API的最佳方法是什么?

Call each promise, one after the other without chaining them together with 'then' clauses. 依次调用每个promise,而不用“ then”子句将它们链接在一起。 Otherwise, if you do they will run serially - which is not what you want. 否则,如果您这样做,它们将连续运行-这不是您想要的。

Use $q.all to register a success/error callback when all promises are resolved or if something goes wrong: 当所有的诺言都已解决或出现问题时,请使用$ q.all注册成功/错误回调:

var p1 = promise1();
var p2 = promise2();
var p3 = promise3();

$q.all([p1, p2, p3])
       .then(
             function() { /*success*/ },
             function() { /*error */ }
        );

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

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