执行承诺之后如何执行该语句? [英] how to execute the statement after promise is executed?

查看:171
本文介绍了执行承诺之后如何执行该语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的目录在我的模板
我想改变下拉列表ID为它的模型值我已经使用为波纹管

I have used the following directory in my template i want to change the model value of drop-down to id for it i have used as bellow

  <md-select flex class="md-select-form" ng-model="education.degree" placeholder="Degree" save-id id="education.degree_id" ">
      <md-option ng-value="degree" ng-repeat="degree in ['High School', 'Associates Degree', 'Bachelor Degree', 'Masters Degree', 'M.B.A', 'M.D', 'Ph.D', 'other']">{{degree}}</md-option>
  </md-select>

名为.directory code

.directory code

    .directive('saveId', function(Profile){
        return {
            require: 'ngModel',
            scope: {
                id: '=',
                requiredParam:'@'
            },        
            link: function(scope, element, attrs, ngModel) {
                console.log("initial loading");
                // view --> model (change to string)            
                ngModel.$parsers.push(function(viewValue){
                    var keepGoing = true;                
                    Profile.getDegreeList().then(function(data) {
                        angular.forEach(data, function(ob){
                            if(keepGoing) {
                                if(ob.degree == viewValue){
                                    scope.id = ob.degree_id;
                                    keepGoing = false;
                                }
                            }
                        });
                        console.log("within promise"+scope.id); //it executes second
                    });
                    console.log(scope.id);  //it executes first      
                    return scope.id;

                });
                return scope.id;

            }
        };
    })

在,如果我尝试返回NG-模式的价值的最后阻止它也不能正常工作

Once if i try to return the value of ng-model in finally block it also not working

    .directive('saveId', function(Profile){
        return {
            require: 'ngModel',
            scope: {
                id: '=',
                requiredParam:'@'
            },        
            link: function(scope, element, attrs, ngModel) {
                console.log("initial loading");
                // view --> model (change to string)            
                ngModel.$parsers.push(function(viewValue){
                               // var id = -1;
                    var keepGoing = true;                
                    Profile.getDegreeList().then(function(data) {
                        angular.forEach(data, function(ob){
                            if(keepGoing) {
                                if(ob.degree == viewValue){
                                    scope.id = ob.degree_id;
                                    keepGoing = false;
                                }
                            }
                        });
                    }).finally(function(res){
                        console.log(scope.id);        
                        return scope.id;                    
                    });
                });
                return scope.id;
            }
        };
    })

我已经使用Profile.getDegreeList()服务通过使用分配的ID相关的下拉元素的 ngModel。$ parsers.push()。问题出在服务使用的情况下,承诺要完成它返回previous分配ID之前。
我想prevent它,需要返回的承诺ID。
如何解决这个问题,请帮助?

i have used Profile.getDegreeList() service to assign the id to relevant drop-down element by using ngModel.$parsers.push(). The problem is in case of service usage before promise going to complete it returns the previous assigned id . i want to prevent it and need to return the promise id. how to solve this issue please help?

推荐答案

您可以使用,这将执行的承诺后执行最后方法。

You can use finally method which will be executed after executing promise.

Profile.getDegreeList()
    .success(function(data)
    {
        angular.forEach(data, function(ob)
        {
            if (keepGoing)
                if (ob.degree == viewValue) {
                    scope.id = ob.degree_id;
                    keepGoing = false;
                }
        });
        console.log("within promise"+scope.id);
    })
    .finally(function(res)
    {
        // your code
    }
);

在这里,你先拨打电话,然后使用的解析器。

Here you go first make a call and then use parsers.

Profile.getDegreeList().then(function(data) {
    angular.forEach(data, function(ob) {
        if (keepGoing) {
            if (ob.degree == viewValue) {
                scope.id = ob.degree_id;
                keepGoing = false;
            }
        }
    });
    ngModel.$parsers.push(function(viewValue) {
        var keepGoing = true;
       return scope.id ;
    });
});

请参阅其他<一href=\"http://stackoverflow.com/questions/23559341/using-success-error-finally-catch-with-promises-in-angularjs\">links太获得更多指导。

See other links too for more guidance.

这篇关于执行承诺之后如何执行该语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆