Angularjs 安格拉斯。选择“具有ng模型属性”时,选择的ng不工作

Angularjs 安格拉斯。选择“具有ng模型属性”时,选择的ng不工作,angularjs,angularjs-ng-model,Angularjs,Angularjs Ng Model,我正在尝试设置ng selected在我的选项元素中,selected属性设置为true,但是选项未被选中,当我从选择中删除ng model时,所有属性都变为工作状态 问题:当我使用模型时,如何选择选项 这里是(选中的不在这里工作) 我的代码: var app = angular.module("plunker", []); app.controller("TestController", ["$scope", function($scope) { $scope.test = 1; $

我正在尝试设置
ng selected
在我的
选项
元素中,
selected
属性设置为
true
,但是
选项
未被选中,当我从
选择
中删除
ng model
时,所有属性都变为工作状态

问题:当我使用模型时,如何选择
选项

这里是(
选中的
不在这里工作)

我的代码:

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

app.controller("TestController", ["$scope", function($scope) {
  $scope.test = 1;
  $scope.array = [
        {"id": 1, "name": "first"}, 
        {"id": 2, "name": "second"}, 
        {"id": 3, "name": "third"}
      ];
}]);
我的模板:

  <body ng-controller="TestController">
    Selected item must be {{ array[test-1].name }}
    <select ng-model="myModel">
      <option value="">Choose item..</option>
      <option ng-repeat="item in array" 
              ng-value="item.id" 
              ng-selected="item.id == test">
        {{ item.name }} ({{item.id == test}})
      </option>
    </select>
  </body>

所选项必须为{array[test-1].name}
选择项目。。
{{item.name}}({{item.id==test})

非常感谢你的帮助

不要将ngSelected与ngRepeat一起使用。选择以下选项:

  <body ng-controller="TestController">
    Selected item must be {{ array[test-1].name }}
    <select ng-model="myModel" ng-options="item.id as item.name for item in array">
      <option value="">Choose item..</option>
    </select>
  </body>

所选项必须为{array[test-1].name}
选择项目。。
不要将
ngSelected
ngModel
从文档中:

注意:
ngSelected
不与
ngModel
指令交互,它只在元素上设置所选属性。如果在选择上使用
ngModel
,则不应在选项上使用
ngSelected
,因为
ngModel
将设置选择值和所选选项

请参阅其他文档:

见Stackoverflow:


ng选择和ng型号相互排除。您的模型应该与选项idfor select匹配。我认为,一旦ng模型从select中删除,您需要ng选项。它选择要选择的值。看这个分叉的-我正要写这个P我认为这是正确的答案!!但是,如果我必须基于ng repeat value保留所选值,该怎么办?检查这个