我在一个有许多行的角度js中有一个列表。我想在列表中选择多行并将其添加到另一个表中。
我的代码是
<li class = "list-group-item listroleitem" ng-repeat='role in roles' ng-click="selectRow(role)
" ng-class="{selectedRole: role === idSelectedRow}">{{role.roleName}}</li>
控制器:
$scope.selectRow = function(name){
$scope.idSelectedRow = name;
};
怎么做?
答案 0 :(得分:1)
由于您控制了所选的行变量,为什么不将它初始化为数组?
$scope.idSelectedRow = [];
$scope.selectRow = function(name){
if( $scope.idSelectedRow.indexOf(name) > -1 )
$scope.idSelectedRow.splice($scope.idSelectedRow.indexOf(name), 1);
else
$scope.idSelectedRow.push( name );
};
然后在css中使用它:
<li class = "list-group-item listroleitem" ng-repeat='role in roles' ng-click="selectRow(role)
" ng-class="{selectedRole: isSelected( role ) }">{{role.roleName}}</li>
$scope.isSelected = function(some_role){
return $scope.idSelectedRow.indexOf(role) >-1;
}
答案 1 :(得分:0)
将您的代码更改为
<li class = "list-group-item listroleitem" ng-repeat='role in roles' ng-click="selectRow(role)
" ng-class="{selectedRole: role.selected==true}">{{role.roleName}}---{{role.selected}}</li>
function TodoCtrl($scope) {
$scope.roles = [{roleName:"abc"}, {roleName:"dc"}]
$scope.name = 'Superhero';
$scope.selectRow = function(role){
role.selected = !role.selected;
};
}