[英]How to get checked checkbox value and store it array variable in angularjs?
I have created array list and listing it as multiple check box. 我创建了数组列表并将其列为多个复选框。 From that if i select i'm storing into the array variable and if I uncheck its need to be remove from array.
从那个,如果我选择我存储到数组变量,如果我取消选中它需要从数组中删除。 I tried this is working for but while uncheck value is not removing from the array variable.
我试过这是有效的,但取消检查值不是从数组变量中删除。
Here is my code below and jsfiddle 这是我下面的代码和jsfiddle
HTML HTML
<div ng-app="myApp" ng-controller="MyCtrl">
<lable ng-repeat="list in lists">
<input type="checkbox" name="chk[]" ng-model="lst" value="{{list.vl}}" ng-change="change()">
{{list.vl}} <br>
</lable>
</div>
SCRIPT 脚本
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope){
$scope.lists = [
{'vl' : 1},
{'vl' : 2},
{'vl' : 3},
{'vl' : 4},
{'vl' : 5},
];
$scope.lst = [];
$scope.change = function(){
$scope.lst.push('2');
console.log($scope.lst);
};
});
You can pass data in ngChange
that you need to decide if you want to push or splice. 您可以在
ngChange
中传递数据,您需要决定是否要推送或拼接。
HTML HTML
<lable ng-repeat="list in lists">
<input type="checkbox" ng-model="active" ng-change="change(list, active)" >
</lable>
SCRIPT 脚本
$scope.change = function(list, active){
if (active)
$scope.lst.push(list);
else
$scope.lst.splice($scope.lst.indexOf(list), 1);
};
Note that the current value for each item is stored in a variable active
on the isolated scope. 请注意,每个项目的当前值存储在隔离范围上的
active
变量中。 If you need the value in your model, just bind likes this: 如果您需要模型中的值,只需绑定如下:
<lable ng-repeat="list in lists">
<input type="checkbox" ng-model="list.active" ng-change="change(list, active)" >
</lable>
https://jsfiddle.net/ruzw4Lfb/8/ https://jsfiddle.net/ruzw4Lfb/8/
Use push and pop of array 使用数组的推送和弹出
While change() send two values first is it selected which is provided by ng-model and second the value you want to push or slice(remove). 而change()首先发送两个值,它是由ng-model提供的,第二个是你要推送或切片(删除)的值。
<input type="checkbox" name="chk[]" ng-model="lst" value="{{list.vl}}" ng-change="change(lst,list.vl)">
$scope.change = function(check,value){
if(check){
$scope.lst.push(value);
}else{
$scope.lst.splice($scope.lst.indexOf(value), 1);
}
};
Hope it help :) 希望它有帮助:)
I also created a solution for your problem in a different way. 我还以不同的方式为您的问题创建了一个解决方案。 I made something simple than yours.
我做的比你的简单。 This may help you.
这可能对你有所帮助。 Check the example code below:
检查下面的示例代码:
app.js app.js
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope){
$scope.lists = [
{'vl' : 1, 'state' : false},
{'vl' : 2, 'state' : false},
{'vl' : 3, 'state' : false},
{'vl' : 4, 'state' : false},
{'vl' : 5, 'state' : false}
];
$scope.change = function(id){
$scope.lists[id].state = !$scope.lists[id].state;
console.log($scope.lists);
};
});
index.html 的index.html
<div ng-app="myApp" ng-controller="MyCtrl">
<lable ng-repeat="list in lists">
<input type="checkbox" value="{{list.vl}}" ng-click="change($index)">
{{list.vl}} <br>
</lable>
</div>
first I assume taht you synthax of list is incorrect, as you should have list s : 首先我假设你的list的synthax是不正确的,因为你应该有list s :
$scope.lists = [
{'vl' : 1},
{'vl' : 2},
{'vl' : 3},
{'vl' : 4},
{'vl' : 5},
];
Then, this is unecessary : $scope.lst = {};
然后,这是不必要的:
$scope.lst = {};
as you can affect the model of each item of your list like : 因为你可以影响列表中每个项目的模型,如:
<input type="checkbox" name="chk[]" ng-model="item.lst" value="{{list.vl}}" ng-change="change()">
note Item.lst that will update yout lists model. 注意将更新yout列表模型的Item.lst。
after several change you will have : 经过多次改变,您将拥有:
$scope.lists = [
{'vl' : 1, 'lst' : value},
{'vl' : 2, 'lst' : value},
{'vl' : 3, 'lst' : value},
{'vl' : 4, 'lst' : value},
{'vl' : 5, 'lst' : value},
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.