當我在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