mardi 23 mai 2017

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=""
    ng-checked="selection.indexOf(r) > -1" ng-click="toggleSelection(r)"> 
</label>

MessageUserList JSON comming from web service as :

{  
   "member":{  
      "member_id":8,
      "first_name":"Mr. David",
      "last_name":"Raz",
      "phone":122,
      "password":"dd",
      "mail":"sushil@asd.com",
      "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)




Aucun commentaire:

Enregistrer un commentaire