Angularjs 如何在angular中向对象内部的数组中添加复选框?
我想将formData中的值存储在week数组中,如何帮助。如何将复选框值以这种格式存储到数组中HTMLAngularjs 如何在angular中向对象内部的数组中添加复选框?,angularjs,Angularjs,我想将formData中的值存储在week数组中,如何帮助。如何将复选框值以这种格式存储到数组中HTML <input type="checkbox" ng-model="formData.week[]" ng-true-value="'Tuesday'"> <input type="checkbox" ng-model="formData.week[]" ng-true-value="'Wednesday'"> <input type="checkbox" ng-
<input type="checkbox" ng-model="formData.week[]" ng-true-value="'Tuesday'">
<input type="checkbox" ng-model="formData.week[]" ng-true-value="'Wednesday'">
<input type="checkbox" ng-model="formData.week[]" ng-true-value="'Thursday'">
<label ng-repeat="role in roles">
<input type="checkbox" checklist-model="user.roles" checklist-value="role.id"> {{role.text}}
</label>
您可以执行以下操作:
var myApp=angular.module('myApp',[]);
myApp.controller('myController',['$scope',函数($scope){
$scope.formData={}
$scope.formData.week=[
“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”
];
//将复选框值存储到数组中的代码
var chkboxes=[];
var c=document.getElementsByTagName('input');
对于(变量i=0;i
您可以使用ng单击
var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl2', function($scope) {
$scope.roles = [
{id: 1, text: 'guest'},
{id: 2, text: 'user'},
{id: 3, text: 'customer'},
{id: 4, text: 'admin'}
];
$scope.user = {
roles: [2, 4]
};
$scope.checkAll = function() {
$scope.user.roles = $scope.roles.map(function(item) { return item.id; });
};
$scope.uncheckAll = function() {
$scope.user.roles = [];
};
$scope.checkFirst = function() {
$scope.user.roles.splice(0, $scope.user.roles.length);
$scope.user.roles.push(1);
};
});
HTML:
你能解释一下你想在这里做什么以及到目前为止你做了什么吗?我想把复选框值存储到一个aaray中。多少天复选框?为什么答案计算错误?并没有解决你们想要的问题吗?嗨,abhi,我通过创建一个复选框数组在我的答案中做了一些更改。您可以在控制台中看到阵列。请检查并让我知道这是否回答了您的问题。
<div ng-repeat = "day in days">
<input type="checkbox" value={{day}} ng-click="toggleItems(day);">{{day}}
</div>
$scope.days = ['Monday','Tuesday',...];
$scope.formData = {week: []};
$scope.toggleItems= function(day){
var index = $scope.formData.week.indexOf(day);
if(index == -1) $scope.formData.week.push(day);
else $scope.formData.week.splice(index, 1);
}
<div ng-repeat="day in formData.week">
<input type="checkbox" ng-model="day.value" for="day.name" />
<span>{{day.name}}</span>
</div>
$scope.formData = {
week: [{
name: "Monday",
value: false
}, {
name: "Tuesday",
value: false
},{
name: "Wednesday",
value: false
},{
name: "Thursday",
value: false
},{
name: "Friday",
value: false
},{
name: "Saturday",
value: false
},{
name: "Sunday",
value: false
}]
};