简体   繁体   English

带有表单提交的数组ng-repeat复选框

[英]array ng-repeat checkbox with form submit

在此处输入图片说明
i want to retrive checkbox value on submit event.i am using mongodb database.console i am getting value like this Tyres,Spares,Accessories.i have made view page based on output.if when i click check box i got error in console TypeError: Cannot assign to read only property 'selected' of 'tyres'.how could i solve this please some one help me out 我想检索提交事件上的复选框值。我正在使用mongodb database.console,我正在获得诸如Tyres,Spares,Accessories之类的价值。我已经基于output制作了视图页面。如果单击复选框,控制台TypeError中出现错误:无法分配为只读的“轮胎”属性“ selected”。如何解决此问题,请有人帮助我

 'use strict'; /** * @ngdoc object * @name test1.Controllers.Test1Controller * @description Test1Controller * @requires ng.$scope */ angular .module('test1') .controller('Test1Controller', [ '$scope', '$http', '$location', '$window', function($scope, $http, $location, $window) { $http.get('***').success(function(data, status, response) { $scope.items = (JSON.stringify(data[0].D_Services).replace(/\\"/g, "")).split(','); console.log($scope.items); }); $scope.check = function(items) { console.log(items); }; } ]); 
 <div ng-controller="Test1Controller" data-ng-init="loadservice()"> <div ng-repeat="item in items"> <input type="checkbox" ng-model="item.selected" ng-true-value="'Y'" ng-false-value="'N'" /> {{item}} </div> <input type="button" name="submit" value="submit" ng-click="check(items)" /> </div> 

how can i grab all selected check box values on submit action only

Result of this code: items.split(',') is an array that does not exists in scope, so it cannot be a writable model for ng-repeat directive. 这段代码的结果: items.split(',')是一个在范围内不存在的数组,因此它不能是ng-repeat指令的可写模型。 You should create an array in your scope like this: 您应该在范围内创建一个数组,如下所示:

$scope.items = (JSON.stringify(data[0].D_Services).replace(/\"/g, "")).split(',');

and use this model in markup 并在标记中使用此模型

<div ng-repeat="item in items">
    ...
</div>

If you need result as a string, you should join it before returning: 如果需要将结果作为字符串,则应在返回之前将其加入:

$scope.check = function(items) {
   console.log(items.join(','));
};

since items is an string like 因为项目是一个像

var items = "john,grace,peter";

and when you perform items.split(',') it will result and array of strings like 当您执行items.split(',')时,将得到和类似字符串的数组

["john","grace","peter"]

and when ng-model trys to set checked status for each element which is error because you can't do operations like 当ng-model尝试为每个元素设置检查状态时,这是错误的,因为您无法执行以下操作

items[1].selected

since items[1] is an array. 因为items [1]是一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM