[英]ng-option in angular 1.3 forgets ng-model when the collection is set asyncronously
我在ng-model中遇到ng-option指令的问题(升级到1.3版本后)。 当我在绑定到ng-option的异步设置集合之前设置模型时,并且该模型是十进制值时(使用整数可以正常工作)。 在FF中没有问题,我在IOS Safari和Chrome Atleast中看到了问题。
如您所见 :
.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
使用track by item.id
工作 。
仅设置ng-model以及设置集合也可以正常工作 。
在发布场景的DOM检查(呈现的选择选项中)中,有2个选项正在获取selected
属性。
当id也是字符串时,也可以正常工作 。
如果您切换回角度1.2.x, 则效果很好
我想知道为什么会这样,或者为什么当ng-model为2.5 v / s 2时行为会有所不同? 我对1.3中的ng-option用法进行任何更改(在更改日志中找不到任何内容)还是总体上做错了吗? 这是错误吗?
任何相关的信息/帮助都非常感谢。
这确实是一个错误,已记录在此处以进行跟踪。 问题本身提到的选项可以暂时解决此问题。 另请注意,1.4.x没有此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.