0
我有一個在ng-repeat中重複選擇選項的表單。在這種形式下,我想爲select元素選擇defualt值。事實上,在第一選擇元素選擇值是「n1」,並且在第二選擇元素選擇值是「n2」。而在拖曳選擇元素defualt值是「n2」。 我的問題是什麼?重複在ng-repeat中的選擇選項Angularjs
function MyCntrl($scope) {
$scope.data = {
orders: [{ s:'' }]
};
$scope.list = [1,2];
$scope.data.orders[0] = "n1";
$scope.data.orders[1] = "n2";
$scope.sizes = [ {code: 1, name: 'n1'}, {code: 2, name: 'n2'}];
$scope.update = function() {
console.log($scope.item.code, $scope.item.name)
}
}
<!doctype html>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
<div ng-controller="MyCntrl">
<div ng-repeat="l in list track by $index">
<select ng-model="data.orders[$index]" ng-change="update()">
<option ng-selected ="data.orders[$index] == size.name" ng-repeat="size in sizes">
{{size.name}}
</option>
</select>
</div>
<select ng-model="data.orders[0]" ng-change="update()">
<option ng-selected ="data.orders[0] == size.name" ng-repeat="size in sizes">
{{size.name}}
</option>
</select>
<select ng-model="data.orders[1]" ng-change="update()">
<option ng-selected ="data.orders[1] == size.name" ng-repeat="size in sizes">
{{size.name}}
</option>
</select>
</div>
</body>
</html>
我只是想在聲明中評論說,'NG重複軌道由$指數default'這是不對的。默認是'track by $ id(obj)'。前者按順序追蹤(如果要在陣列中移動東西,則易碎)。後者通過身份追蹤,並給每個項目一個$$ hasKey讓他們分開。 如果您想允許數組中的重複項目,或者如果您確定永遠不會觸摸數組順序,那麼通過$ index進行跟蹤可能只是一個好主意。 – ippi