Prashanth Harish
Prashanth Harish

Reputation: 178

Angular JS checkbox is not working

Am breaking my head from 2 hours. Since am new to angular JS.

I have following checkbox and I want checked checkbox value pushed to an array. If I unchecked it should pop out from an array. I tried following code. But not working...

    <label ng-repeat="r in MessageUserList">
    <input type="checkbox" name="selectList[]" value="{{r}}"
    ng-checked="selection.indexOf(r) > -1" ng-click="toggleSelection(r)"> 
{{r.member.first_name}}</label>

MessageUserList JSON comming from web service as :

{  
   "member":{  
      "member_id":8,
      "first_name":"Mr. David",
      "last_name":"Raz",
      "phone":122,
      "password":"dd",
      "mail":"[email protected]",
      "system_date":"May 18, 2017 3:26:01 PM",
      "society_id":1,         
   },
   "flat_assign":"N"
}

In my controller :

var selection=[];
 // Toggle selection for a given r by name
  $scope.toggleSelection = function toggleSelection(r) {
    var idx = $scope.selection.indexOf(r); //Error here indexOf(r)

    // Is currently selected
    if (idx > -1) {
        alert("Same checked");
      $scope.selection.splice(idx, 1);
    }

    // Is newly selected
    else {
        alert("New checked");
        $scope.selection.push(r);
        alert(JSON.stringify(selection));
    }
  }

Am getting error: TypeError: Cannot read property 'indexOf' of undefined at b.toggleSelection (controllers.js:129)

Upvotes: 0

Views: 131

Answers (2)

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

Try this,

<!DOCTYPE html>
<html>
<head>
    <script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
    <script type="text/javascript">
        var app = angular.module('myApp', []);

        app.controller('surveyController', function($scope){
          $scope.MessageUserList =          [{  
           "member":{  
              "member_id":8,
              "first_name":"Mr. David",
              "last_name":"Raz",
              "phone":122,
              "password":"dd",
              "mail":"[email protected]",
              "system_date":"May 18, 2017 3:26:01 PM",
              "society_id":1,         
          },
          "flat_assign":"N"
      }];

      $scope.selection = [];
 // Toggle selection for a given r by name
 $scope.toggleSelection = function toggleSelection(r) {
    var idx = $scope.selection.indexOf(r); //Error here indexOf(r)

    // Is currently selected
    if (idx > -1) {
        alert("Same checked");
        $scope.selection.splice(idx, 1);
    }

    // Is newly selected
    else {
        alert("New checked");
        $scope.selection.push(r);
        alert(JSON.stringify($scope.selection));
    }
}
});


</script>
</head>
<body ng-controller="surveyController" ng-app="myApp">
   <label ng-repeat="r in MessageUserList">
    <input type="checkbox" name="selectList[]" value="{{r}}"
    ng-checked="selection.indexOf(r) > -1" ng-click="toggleSelection(r)"> 
    {{r.member.first_name}}</label>
</body>
</html>

Upvotes: 1

Sergaros
Sergaros

Reputation: 831

use something like this:

//html

<label>Check me to check both: <input type="checkbox" ng-model="ch" ng-changed="saveChange()"></label>
<br/>
{{ch}}

//js

.controller('yourCtrl', function($scope){
    $scope.ch = false;
    $scope.selection=[];
    $scope.saveChange = ()=>{
         $scope.selection.push(ch);
    }
})

Upvotes: 0

Related Questions