【发布时间】:2014-02-20 05:32:19
【问题描述】:
我想使用 AngularJS 获取复选框的所有选定对象。
下面是我的代码
我的观点.tpl.html
<tr ng-repeat="item in itemList">
<td>
<input type="checkbox" ng-click="clickedItem(item.id)"
ng-model="model.controller.object"
{{item.name}} />
</td>
我的控制器
$scope.itemList = [
{
id:"1",
name:"first item"
},
{
id:"2",
title:"second item"
},
{
id:"3",
title:"third item"
}
];
$scope.selection = [];
$scope.clickedItem = function(itemId) {
var idx = $scope.selection.indexOf(itemId);
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
var obj = selectedItem(itemId);
$scope.selection.push(obj);
}
};
function selectedItem(itemId) {
for (var i = 0; i < $scope.itemList.length; i++) {
if ($scope.itemList[i].id === itemId) {
return $scope.itemList[i];
}
}
}
这里我会得到$scope.selection中所有选中的项目。我怎样才能把它送到ng-model?
是否可以像ng-model="model.controller.object = selection" 那样做,因为我需要将选定的$scope.selection 分配给model.controller.object
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope angularjs-ng-repeat angular-ui