答案 0 :(得分:1)
如果要更改项目的顺序,则需要在单独的数组中跟踪位置。
这是一个工作示例
angular.module("app", []).controller("myCtrl", function($scope){
$scope.tasks = [{
Titel: "Title1",
Position: "9"
},{
Titel: "Title2",
Position: "1"
},{
Titel: "Title3",
Position: "5"
},{
Titel: "Title4",
Position: "7"
}];
$scope.positions = ["1","2","3","4","5","6","7","8","9"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ms-authoringcontrols"><b>Title</b></td>
<td class="ms-authoringcontrols"><b>Position</b></td>
</tr>
<tr ng-repeat="t in tasks | orderBy: 'Position'">
<td>
<a href="TODO">{{t.Titel}}</a>
</td>
<td>
<select ng-model="t.Position" ng-options="i for i in positions"></select>
</td>
</tr>
</table>
</div>
答案 1 :(得分:1)
您非常接近,您要查找的ngOptions格式为select as label for value in array
,其中select
是将分配给模型的值。
在你的情况下,Position是字符串,所以我们需要将select
转换为字符串,以便它与模型匹配:
ng-options="(tasks.indexOf(i)+1).toString() as tasks.indexOf(i)+1 for i in tasks"
^ model value ^ display
(tasks.indexOf(i)+1+''
也可以)
angular
.module('Test', [])
.controller('TestCtrl', function($scope) {
$scope.tasks = [{
Titel: "Title1",
Position: "9"
},{
Titel: "Title2",
Position: "1"
},{
Titel: "Title3",
Position: "3"
},{
Titel: "Title4",
Position: "2"
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="Test" ng-controller="TestCtrl">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td class="ms-authoringcontrols"><b>Title</b></td>
<td class="ms-authoringcontrols"><b>Position</b></td>
</tr>
<tr ng-repeat="t in tasks">
<td>
<a href="TODO">{{t.Titel}}</a>
</td>
<td>
<select ng-model="t.Position" ng-options="(tasks.indexOf(i)+1).toString() as tasks.indexOf(i)+1 for i in tasks"></select>
</td>
</tr>
</table>
<pre>{{tasks|json}}</pre>
</div>
(如果您运行代码段,您会注意到第一个不是 选中,那是因为[1,2,3,4]不包含9。)