0

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)

7
  • Is your $scope.selection instantiated in your controller or somewhere else in your code ? Commented May 23, 2017 at 8:29
  • change var selection=[]; to $scope.selection=[]; may help. :) Commented May 23, 2017 at 8:30
  • @Radouane I just declared as var selection=[];. Commented May 23, 2017 at 8:36
  • As @Pengyy mentionned, you have to declare it within you $scope. Please refer to Pengyy comment. Commented May 23, 2017 at 8:37
  • Yes I declared using $scope.. Commented May 23, 2017 at 8:38

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

I changed var selection=[]; to $scope.selection=[]; and changed same in alert also :)
0

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);
    }
})

Comments

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.