【问题标题】:how to execute the statement after promise is executed?promise执行后如何执行语句?
【发布时间】:2016-03-11 23:55:35
【问题描述】:

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

.目录代码

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

    .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。 请问这个问题怎么解决?

【问题讨论】:

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


    【解决方案1】:

    你可以使用finally方法,它会在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
        }
    );
    

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

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

    请参阅其他 links 以获得更多指导。

    【讨论】:

    • 我们可以在 finally 块中返回一个值吗?
    • 你可以从scope中获取值
    • 你能帮我解决这个问题吗?
    • 所以你想返回 id ??不是吗
    • 我想返回我在帖子中提到的想要更新 ng-model 值的 id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2020-11-21
    • 1970-01-01
    相关资源
    最近更新 更多