承诺执行后如何执行语句?

时间:2015-12-07 11:33:01

标签: angularjs angularjs-directive angularjs-scope angular-promise angularjs-ng-model

我在模板中使用了以下目录 我想将下拉列表的模型值更改为id,因为我已将其用作下面的文件

  <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

    .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-model的值,那么它也无法正常工作

    .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。 我想阻止它,并需要返回promise id。 如何解决这个问题请帮帮忙?

1 个答案:

答案 0 :(得分:1)

您可以使用[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; 方法,该方法将在执行promise后执行。

finally

首先,您先打电话然后使用解析器。

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

有关更多指导,请参阅其他links