Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a checkbox.

If checked -> call one function displayBus(number)

If unchecked -> call other function displayCar()

The problem with this code is that I have to click also in hide or show to call the function.

  <input type="checkbox" id="mycheckbox" name="mycheckbox" ng-model="mycheckbox" />
        <div ng-show="mycheckbox">
             <a style="cursor: pointer;" ng-click="displayBus(number);">hide</a>

        </div>
        <div  ng-hide="mycheckbox">
            <a style="cursor: pointer;" ng-click="displayCar();">show</a>
        </div>

But I just want to check / Uncheck and then call automaticly the function. I DO NOT want to click on hide or show.

Thanks for your help.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You can wire up an ng-change to the checkbox and then check it's value:

<input type="checkbox" id="mycheckbox" name="mycheckbox" ng-model="mycheckbox" data-ng-change="dataChanged()" />

and then in your javascript:

$scope.dataChanged = function(){
    if($scope.mycheckbox){
        //checked
    }
    else{
        //not checked
    }
};

Edit:

If you want to do it only in html:

<a style="cursor: pointer;" ng-click="myCheckbox ? displayBus(number) : displayCar()">hide</a> 

Edit:

<input type="checkbox" id="mycheckbox" name="mycheckbox" ng-model="mycheckbox" data-ng-change="myCheckbox ? displayBus(number) : displayCar()" />
share|improve this answer
    
Thanks but better if it is not in JavaScript but HTML –  Carlos Oct 29 '14 at 12:59
    
See latest edit –  Mathew Berg Oct 29 '14 at 13:06
    
I need to have check/uncheck. Then it will execute displayBus(number) or displayCar() –  Carlos Oct 29 '14 at 13:24
    
Please describe your question a little better as I am confused on what you're trying to accomplish. –  Mathew Berg Oct 29 '14 at 13:25
    
Just edited the question, thank. –  Carlos Oct 29 '14 at 13:31

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.