angular 1.3中的ng选项在集合异步设置时忘记了ng模型

ng-option in angular 1.3 forgets ng-model when the collection is set asyncronously

本文关键字:ng 设置 异步 模型 忘记了 集合 中的 选项 angular      更新时间:2024-06-14

在升级到1.3版本后,我遇到了一个使用ng模型的ng选项指令的问题。当我在异步集集合之前设置的模型绑定到ng选项时,以及当模型是十进制值时(适用于整数)。在FF中没有问题,我在IOS safari和chrome中看到了问题。

如您所见请参见此处

.controller('MainCtrl', function($scope , $timeout) {
   $scope.selected = {item: 2.5}; //setting an integer options works just fine example item:2
   //Returns a promise which is resolved with the data/async call simulation
   function getData(){
     return $timeout(function(){
       return [{value:"0",id:0},{value:"0.5",id:0.5},{value:"1",id:1},{value:"1.5",id:1.5},{value:"2",id:2},{value:"2.5",id:2.5},{value:"3",id:3}]
     });
   }
  getData().then(function(data){
      $scope.items = data;  
  });
});

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope , $timeout) {
  $scope.selected = {item: 2.5}; //setting an integer options works just fine example item:2
  
  function getData(){
    return $timeout(function(){
     return [{value:"0",id:0},{value:"0.5",id:0.5},{value:"1",id:1},{value:"1.5",id:1.5},{value:"2",id:2},{value:"2.5",id:2.5},{value:"3",id:3}]
      
    });
  }
  
  getData().then(function(data){
      $scope.items = data;  
  });
  
});
<!DOCTYPE html>
<html ng-app="plunker">
  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.11/angular.js" data-semver="1.3.14"></script>
    <script src="app.js"></script>
  </head>
  <body ng-controller="MainCtrl">
   {{selected.item}}
   <select ng-model="selected.item" ng-options="item.id as item.value for item in items"></select>
  </body>
</html>

一些观察:

  • 使用track by item.id效果良好。

  • 只在设置集合的同时设置ng模型也很好。

  • 2个选项是在问题场景的DOM检查(渲染的选择选项)上获取selected属性。

  • 当id为字符串时也可以正常工作。

  • 如果您切换回角度1.2.x,则效果良好

我想知道为什么会发生这种情况,或者为什么当ng模型为2.5 v/s2时,它的表现会有所不同?我是否对1.3中的ng选项用法做了任何更改(在更改日志中找不到任何内容)或一般情况下出错?这是个虫子吗?

非常感谢任何相关信息/帮助。

这确实是一个错误,已记录在此处进行跟踪。问题中提到的选项本身可以暂时解决这个问题。还要注意,1.4.x没有这个问题。