Angular ngSelected在版本> 1.3中不起作用

bra*_*orf 0 html javascript angularjs

标记:

<!DOCTYPE html>
<html ng-app="test">

  <head>
    <script data-require="angular.js@1.5.6" data-semver="1.5.6" src="https://code.angularjs.org/1.5.6/angular.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="ctrl">
    <h1>Hello Plunker!</h1>
    <select ng-model="user.item_id">
      <option ng-selected="i.id == user.item_id" ng-repeat="i in items" value={{i.id}}>{{i.name}}</option>
    </select>
  </body>

</html>
Run Code Online (Sandbox Code Playgroud)

JS:

var module = angular.module("test", []);

module.controller('ctrl', function($scope){

  $scope.items = [
    {id: 1, name: 'foo'},
    {id: 2, name: 'bar'},
    {id: 3, name: 'baz'},
  ];

  $scope.user = {};
  $scope.selectedItem = {id: 1};

  $scope.user.item_id = $scope.selectedItem.id;


});
Run Code Online (Sandbox Code Playgroud)

Plunker:https://plnkr.co/edit/7oi4KwzMhGi3kdltSklg p = preview

问题:如果你检查了它的html代码select,你会看到HTML selected属性被正确放置.

但是,它不会显示为突出显示的选项.为什么?

==编辑==

那个plunker代码在角度1.3.20上按预期工作,但它在1.4.x或1.5.x中被破坏

工作人员:https://plnkr.co/edit/0ApQeZ6Kar2yQisELXfT p = preview

== EDIT2 ==

我在angularjs队列上发了一张票:https://github.com/angular/angular.js/issues/14876#issuecomment-231010972

基本上,他们说我们应该坚持使用ngOptions,尽管他们不知道为什么ngSelected会被打破.

小智 5

好吧,你可以使用ng-options代替......

<select ng-model="user.item_id" ng-options="i.id as i.name for i in items">
</select>
Run Code Online (Sandbox Code Playgroud)

点击这里 https://plnkr.co/edit/G4Hu4ZpShaUPCE5zTsdV