我按照这个SO-link尝试使用它。如果我传递与答案中相同的数组,我得到结果。现在我尝试用我的输入替换数组,如下所示,但我得到[对象对象]。我试图将js对象转换为json但我收到错误。该指令不允许它。如何获得理想的结果。
在控制器中
App.controller('PartsController',['$scope', 'Parts', function($scope,Parts) {
$scope.parts = {};
$scope.availablePartsTemp = [];
$scope.availableParts = [];
$scope.fetchAllParts = function() {
$scope.availablePartsTemp = Parts.resource.query(function(
response) {
angular.forEach(response, function(item) {
if (item) {
$scope.availableParts .push(item);
}
});
});
};
$scope.selectedList = [];
} ]);
在html中
<ng-list-select selected-list="selectedList"
key="part" available-list="availableParts" ></ng-list-select>
<br/>
<b>availableList::</b>{{availableParts | json}}
<br />
<b>selectedList::</b>{{selectedList| json}}
答案 0 :(得分:0)
我没有确切地知道你面临的问题,我已经创建了一个类似于你的类似的模型,它似乎工作得很好。
请看一下
<强> Working Plunker 强>
<强>脚本强>
angular.module('app', ['ngListSelect'])
.controller('MainCtrl', function($scope, $q) {
//////////// Mocking Parts //////////////
Parts = function(){};
Parts.resource = function(){};
Parts.resource.query = function(){
deferred = $q.defer();
var responseJson = [{id:1, part:'Hose'},{id:2, part:'Clamp'}, {id:3, part:'Pipe'}, {id:4, part:'Screw'}, {id:5, part:'Hammer'}]
deferred.resolve(responseJson);
return deferred.promise;
};
//////////// Mocking Finished ///////////
$scope.parts = {};
$scope.availablePartsTemp = [];
$scope.availableParts = [];
$scope.fetchAllParts = function()
{
$scope.availablePartsTemp = [];
Parts.resource.query().then(function(response) {
angular.forEach(response, function(item) {
if(item)
{
$scope.availableParts.push(item);
}
});
},
function(err){
console.log(err);
});
};
$scope.selectedList = [];
$scope.fetchAllParts();
});
<强> HTML 强>
<body ng-app="app">
<div ng-controller="MainCtrl">
<br/>
<ng-list-select selected-list="selectedList" key="part" available-list="availableParts"></ng-list-select>
<br/>
<b>availableList::</b>{{availableParts | json}}
<br />
<b>selectedList::</b>{{selectedList| json}}
</div>
</body>