当我在select
中使用ng-options
循环列表并生成我的下拉列表时,是否可以将动态属性id
添加到option
?它工作,如果我使用ng-repeat
循环,因为我可以将attribut id
直接放在option
标签中,并用$index
递增。这也可能与ng-options
?这里是我的代码:在ng选项中添加动态ID到选项
angular.module("myApp", []).controller("myController", function($scope) {
$scope.selectedOption1;
$scope.selectedOption2;
$scope.myList = [{
text: "1"
}, {
text: "2"
}, {
text: "3"
}, {
text: "4"
}, {
text: "5"
}, ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<!--BUILD DROPDOWN WITH NG-OPTIONS LOOP-->
<select ng-model="selectedOption" ng-options="option as option.text for option in myList"></select>
<!--BUILD DROPDOWN AND SET ATTRIBUT ID WITH NG-REPEAT LOOP-->
<select ng-model="selectedOption2">
<option id="option_{{$index}}" ng-repeat="option in myList">{{option.text}}</option>
</select>
</div>
两个select
的截图(红色边框的ID用):
任何想法?谢谢。
对于'ng-options',你只能设置'option'的'text'和'value'而不是'id'。 – Aruna
@Aruna好吧我期待这样的答案 - 谢谢。 :-) – MrBuggy
@Aruna如何修改ng选项循环中的值?目前,每个选项的值为0-5(请参见屏幕截图)。我可以在那里设置一个文本,并像ng-repeat一样用$ index增加它?谢谢 – MrBuggy