使用异步函数的ng-model奇怪行为

时间:2015-03-27 11:07:18

标签: javascript angularjs angular-ngmodel

下面是下拉选择代码:

<select ng-model="filter.area">
    <option value="">Select your location</option>
    <option ng-repeat="area in areaNames" value='{{ area.area_name }}'>{{ area.area_name }}</option>
</select>
在调用异步函数之前,在控制器中设置了

ng-model

$scope.filter.area = $cookies['filterArea'];
// which evaluated to some value lets say 'Bole'

现在出现了异步功能:

query.find({
   success: function(results_area) {
     $scope.$apply(function() {

        $scope.areaNames = results_area;

        for (var i = 0; i < $scope.areaNames.length; i++) {
          $scope.areaNames[i].area_name = $scope.areaNames[i].get('name');
        };

      });
   },
   error: function(error) {
      console.log("error in fetching area info....");
    }
   });

ng-model未更新。这里有什么想法错了吗?

要观看演示,请访问: http://peppy-avatar-762.appspot.com/

然后选择“亚的斯亚贝巴”并选择“博乐”作为区域,然后点击查找食物!检查左侧的区域过滤器。 ng-model没有更新!

1 个答案:

答案 0 :(得分:2)

如果选项是懒惰加载的,请使用ng-options代替ng-repeat上的option

<select data-ng-model="selectedItem" 
        data-ng-options="item for item in items track by item">
</select>

Working Plnkr