在AngularJS中使用ng-options时,我遇到了一个奇怪的问题。
我的情景很简单:
从后端加载的对象是一个键/值,如:
{
Value: "my_value",
Name: "my_name"
}
一切正常...... 直到名称和价值相同。然后,角度不会再将所选值绑定为正确。
我创建了这个plunker: https://plnkr.co/edit/tUBXIpMTBAHK2Xh8aDu6?p=preview 为了证明这个问题。
我的控制器:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $timeout) {
$scope.name = 'World';
$timeout(function(){
$scope.values = [
{ Name: "Accepted", Value: "Accepted" },
{ Name: "Not accepted", Value: "NotAccepted" },
{ Name: "Not at all accepted", Value: "NotAtAllAccepted" }
];
}, 2000);
$scope.selectedValue = "Accepted";
//$scope.selectedValue = "NotAccepted";
//$scope.selectedValue = "NotAtAllAccepted";
});
如果plunker示例中的选定值设置为' Accepted'不起作用。但是,如果将所选值设置为某些其他值 - 那么它可以正常工作。
我的HTML:
<!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.20/angular.js" data-semver="1.3.20"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{selectedValue}}!</p>
<select ng-model="selectedValue" ng-options="orderStatus.Value as orderStatus.Name for orderStatus in values"></select>
</body>
</html>
任何可以在这个上启发我的人?
谢谢!
更新
看起来它在Firefox中运行良好。但在Chrome和IE中,问题仍然存在。
答案 0 :(得分:2)
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $timeout) {
$scope.name = 'World';
$timeout(function(){
$scope.values = [
{ 'Name': "Accepted", 'Value': "Accepted" },
{ 'Name': "Not accepted", 'Value': "NotAccepted" },
{'Name': "Not at all accepted", 'Value': "NotAtAllAccepted" }
];
$scope.selectedValue = "Accepted";
}, 2000);
//$scope.selectedValue = "NotAccepted";
//$scope.selectedValue = "NotAtAllAccepted";
});
&#13;
<!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.20/angular.js" data-semver="1.3.20"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{selectedValue}}!</p>
<select ng-model="selectedValue" ng-options="orderStatus.Name as orderStatus.Value for orderStatus in values"></select>
</body>
</html>
&#13;