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

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

问题描述

我在模板中使用了以下目录我想将下拉列表的模型值更改为 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>

.目录代码

    .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;

            }
        };
    })

如果我尝试在 finally 块中返回 ng-model 的值,它也不起作用

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() 服务通过使用 ngModel.$parsers.push() 将 id 分配给相关的下拉元素.问题是在承诺完成之前服务使用的情况下,它返回先前分配的 id .我想阻止它并需要返回承诺 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?

推荐答案

可以使用 finally 方法,该方法会在执行 promise 后执行.

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 ;
    });
});

查看其他链接以获得更多指导.

See other links too for more guidance.

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

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