0

enter image description here
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

'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

2 Answers 2

1

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. 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(','));
};
Sign up to request clarification or add additional context in comments.

3 Comments

thanks i have changed my code.<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()"/> .how can i take only selected values on submit event
Use 'map' method to convert 'items' array to format like this: [ { value: 'Tyres', selected: false },... ]. and bind checkbox text to 'item.value' like this: {{item.value}} . Than you can filter your values by 'filter' method before sending.
PS: Just google for Array.prototype.map() and Array.prototype.filter() if you don`t know how to use it.
0

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

["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

items[1].selected

since items[1] is an array.

1 Comment

how can i take selected values on submit event

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.