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.

2
  • What exactly are you trying to achieve here? Also is there a reason you pass the $index instead of the actual model you want to change? Commented May 9, 2017 at 9:53
  • when i check the checkbox i want to show true else false using index Commented May 9, 2017 at 9:54

4 Answers 4

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

8 Comments

there is no way show any new scope value like active?
@jose In your code, by $scope.Names[index].active='true'; you are trying to update active property. isn't it?
$scope.Names[0].active=true/false i create dynamic like this @Pengyy
@jose sorry.....not at all. maybe we should chat. chat.stackoverflow.com/rooms/143769/angularjs-checkbox-ng-click
|
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.

Comments

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.

6 Comments

no @philip i need that active scope not use model name :(
That active attribute does not hold the value of your model though.
i create active scope in run time
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.
my ng-model is look like this how to i call ng-model="selection.id[BFMaterialStream.MaterialStream]"
|
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>

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.