我想使用下面的代碼在兩個選擇列表之間移動項目,但項目不會從可用客戶端列表移動到selectedClients列表,因此有人可以檢查下面的代碼並讓我知道我在這裏錯過了什麼?由於AngularJS在兩個選擇列表之間移動項目
<div ng-app>
<div ng-controller="testCtrl">
<label for="aclients">Available Clients</label>
<select size="5" multiple ng-model="available" ng-options="client.id as client.Name for client in clientsList" style="width: 400px"></select>
<input id="moveright" type="button" value="Add Client" ng-click="moveItem(available[0], availableclients,selectedclients)" />
<input id="moverightall" type="button" value="Add All Clients" ng-click="moveAll(availableclients,selectedclients)" />
<input id="move left" type="button" value="Remove Client" ng-click="moveItem(selected[0], selectedclients,availableclients)" />
<input id="moveleftall" type="button" value="Remove All Clients" ng-click="moveAll(availableclients,selectedclients)" />
<label for="sclients">Selected Clients</label>
<select size="5" multiple ng-model="selected" ng-options="client.id as client.Name for client in selectedclients" style="width: 400px"></select>
<div>Selected Clients IDs: {{selectedclients}}</div>
</div>
</div>
控制器:
app.controller('testCtrl',
function testCtrl($scope, clientsService){
$scope.clientsList = clientsService.getClientsList().then(
function(response){
$scope.clientsList = response;
},
function(status){
console.log(status);
}
);
$scope.moveItem = function(item, from, to) {
console.log('Move item Item: '+item+' From:: '+from+' To:: '+to);
//Here from is returned as blank and to as undefined
var idx=from.indexOf(item);
if (idx != -1) {
from.splice(idx, 1);
to.push(item);
}
};
$scope.moveAll = function(from, to) {
console.log('Move all From:: '+from+' To:: '+to);
//Here from is returned as blank and to as undefined
angular.forEach(from, function(item) {
to.push(item);
});
from.length = 0;
};
$scope.availableclients = [];
$scope.selectedclients = [];
});
感謝您的幫助,您提供的示例工作得很好,但只有當我第一次將從$ scope.clientsList返回的結果集傳遞給$ scope.availableclients時。如果我嘗試從$ scope.clientsList加載,就像我最初在我的問題中做的那樣,它不會加載可用客戶端列表中的任何數據.....所以我錯過了什麼/在這裏做錯了什麼?在此先感謝 – MChan
這是其中一個問題:持續使用可用客戶端,或持續使用clientsList,但不是兩者。 –
哦,我看到了,非常感謝你的澄清,感謝它 – MChan