亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

angular.js - angualrjs, how to write the next step after the $http asynchronous operation is executed
大家講道理
大家講道理 2017-05-15 17:05:30
0
3
909

There are three steps
step1: $http.jsonp(url1)
step2: $http.jsonp(url2)
step3: assignment operation,
There is no order requirement for steps 1 and 2. 3 is required to be executed after steps 1 and 2 are completed;

Because steps 1 and 2 will be called in many places, we don’t want it to be

步驟1.success{
   步驟2.success{
      步驟3}} 這樣的寫法    

I hope to encapsulate steps 1 and 2 into a public method, and then execute step 3 sequentially. How should I write it in angularjs

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

reply all(3)
Peter_Zhu

Use events. Don’t use nesting

$scope.$on('step1success',function(){
    //步驟二代碼
//執(zhí)行完成后在回調(diào)函數(shù)中觸發(fā)
    $scope.$emit('step2success');
});
$scope.$on('step2success',function(){
    //步驟3代碼
//執(zhí)行完成后在回調(diào)函數(shù)中觸發(fā)
    $scope.$emit('step3success');
});
$scope.$on('step3success',function(){
    //全部執(zhí)行完成
});

//步驟一代碼
//執(zhí)行完成后在回調(diào)函數(shù)中觸發(fā)
$scope.$emit('step1success');
曾經(jīng)蠟筆沒有小新

Use the $q service that comes with ng

let promises = {
    alpha: promiseAlpha(),
    beta: promiseBeta(),
    gamma: promiseGamma()
}
$q.all(promises).then((values) => {
    console.log(values.alpha); // value alpha
    console.log(values.beta); // value beta
    console.log(values.gamma); // value gamma

    complete();
});
// promises包含多個promise對象,當(dāng)所有promise對象成功返回時,$q.all().then()中的成功方法才會被執(zhí)行。

//  $http返回的正是promise對象
曾經(jīng)蠟筆沒有小新

The author can learn about $q and promise objects. As shown above, Angular has $q.all(), which you can use.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template