Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Is there a way to pass to angular directive ng-click the value of the associated input?

In other words, what should replace this.value in the following:

<input type="checkbox" id="cb1" ng-click="checkAll(this.value)" />

PS. I don't want a workaround to alternate values, I just wonder if is possible to pass a value of an input as argument to a angular function.

share|improve this question
    
Can you please add some more code what in you want in checkAll – Mitul Oct 30 '15 at 8:30
    
add a model on input and pass it in method – ngLover Oct 30 '15 at 8:35
    
Possible duplicate of Angular checkbox and ng-click – Kulbhushan Oct 30 '15 at 9:23
up vote 6 down vote accepted

You can do the following things:

  • Use ng-model if you want to bind a html component to angular scope
  • Change ng-click to ng-change
  • If you have to bind some value upon checking and un-checking the checkbox use ng-true-value="someValue" and ng-false-value="someValue"

The order of execution of ng-click and ng-model is ambiguous since they do not define clear priorities. Instead you should use ng-change or a $watch on the $scope to ensure that you obtain the correct values of the model variable.

Courtesy of musically_ut

Working Demo

<input type="checkbox" id="cb1" ng-model="check" ng-change="checkAll(check)" ng-true-value="YES" ng-false-value="NO"/> Citizen
share|improve this answer

Use ng-model:

<input type="checkbox" id="cb1" ng-change="checkAll(checkVal)" ng-model="checkVal"/>
share|improve this answer

I'm not sure it's a good idea to do it, but here is how it can be achieved :

<input type="checkbox" id="cb1" ng-click="checkAll(myModel)" ng-model="myModel" />
share|improve this answer

Assigning an ng-model to it will return boolean value depending upon the change to it:

<input type="checkbox" id="cb1" ng-model="checkValue" ng-change="checkAll(checkValue)" />

Better to use ng-change rather than ng-click

share|improve this answer
    
There is no much sense to pass the checkValue as argument, cause it anyway available in the model... – Serge Oct 30 '15 at 8:34
    
@Serge Yes there is, it allows you to control the flow of data, and if someone else has to read the code it will be much easier to understand where does that data came from. – ThibaudL Oct 30 '15 at 8:53

Bind the checkbox to a value and pass it to ng-click.

<input type="checkbox" ng-model="value" id="cb1" ng-click="checkAll(value)" />
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.