0

I have checkbox inside the ng-repeat I want check whether checkbox is checked or not using their index value

html

<div ng-repeat="Name in Names">
    <input type="checkbox" 
           ng-change="checkchange(Name .MaterialStream, $index)"
           ng-model="Name.MaterialStream" />
</div>

controller

$scope.checkchange=function(index){
      $scope.Names[index].active='true';
}

Now I get correct value when I check the checkbox. I have to get active value is true but I have check means active should be change into false in my case it's true here I attached my code help how to do this.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
jose
  • 1,044
  • 1
  • 12
  • 35

4 Answers4

2

you can bind Name.active directly without using ng-repeat.

<div ng-repeat="Name in Names">         
  <input type="checkbox" ng-model="Name.active" />
</div>

Since you are binding Name.MaterialStream to checkbox, you can set Name.active based on it in ng-change.

$scope.checkchange=function(index){
  $scope.Names[index].active = $scope.Names[index].MaterialStream;
}
Pengyy
  • 37,383
  • 15
  • 83
  • 73
1

To get the value of your checkbox, you simply have to return the model itself.

HTML

<div ng-repeat="Name in Names">
    <input type="checkbox" ng-change="checkchange($index)"
           ng-model="Name.MaterialStream" />
</div>

JS

$scope.checkchange=function(index){
    return $scope.Names[index].MaterialStream;
}

Note that I removed the first argument you passed to the checkchange function as it wasn't used before. In fact, before you checked the index variable but it wouldn't hold the real $index-value.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
  • no @philip i need that active scope not use model name :( – jose May 09 '17 at 10:02
  • That `active` attribute does not hold the value of your model though. – Philipp Meissner May 09 '17 at 10:02
  • i create active scope in run time – jose May 09 '17 at 10:06
  • I really do not see the point what you want to achieve. Do you want to have the `active` attribute hold the models state (true/false)? If so, you just need to change the ng-model as described in @Pengyys answer. – Philipp Meissner May 09 '17 at 10:07
  • my ng-model is look like this how to i call ng-model="selection.id[BFMaterialStream.MaterialStream]" – jose May 09 '17 at 10:11
  • That is completely off from what you posted before. If you want to access `Name.active`, just set it like so. I think you should share all of your code or at least more parts of it, because otherwise I don't see a way to solve your problem completely. – Philipp Meissner May 09 '17 at 10:24
1

ng-model is more than enough to store true false for checkbox, you don't require $index at all, but If you must do it by $index, then also you can utilize the ng-model.

See this Fiddle example out of your code.

In addition: in your above code, definition of checkchange() method doesn't accept the first argument, thats an issue. you shouldn't pass it if you not going to consume it.

anoop
  • 3,812
  • 2
  • 16
  • 28
0

Here is your Solution . With working example

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.Names = [{value : true}, {value : true},{value : false},{value : false},{value : true}]
        $scope.toggleSelection = function(x,index,event) {
          // how to check if checkbox is selected or not
          //alert(index + ' is ' + event.target.checked);  
          $scope.Names[index].Active = $scope.Names[index].value;
        };
});
<body ng-app="myApp" ng-controller="myCtrl">
  <div ng-repeat="Name in Names">
   {{$index}}
   <input type="checkbox" id="" name="check_box" 
          value="{{x}}"
          ng-change="toggleSelection(x.value,$index,$event)"
          ng-model="Name.value" > {{Name.Active}}
  </div>
  
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
georgeawg
  • 48,608
  • 13
  • 72
  • 95