2

How can I execute a function when a check box is checked in angular js. I have seen a few answers on stack overflow regarding this but I cannot seem to implement them on my scenario

my code:

<div ng-controller="MyCtrl">
 <div class="checkbox">
        <label>
        <input type="checkbox" ng-model="thirtyDay" ng-click="changeAxis()">
        Last 30 Days
        </label>
         </div>
         <div class="checkbox">
        <label>
        <input type="checkbox" ng-model="wholeTimeline" ng-click="changeAxis()">
        Whole Timeline
        </label>
         </div>
</div>

js.

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
   function changeAxis(){
          if ($scope.thirtyDay) {
                    alert("checked 30");
                }
          else if($scope.wholeTimeline) {
                    alert("checked whole");
                }

        };
}
0

3 Answers 3

5

You need to place the function on the $scope, so the view will recognize it:

$scope.changeAxis = function() {
      if (!$scope.thirtyDay) {
           alert("checked 30");
      }
      else if(!$scope.wholeTimeline) {
           alert("checked whole");
      }
 };

Another thing to notice is that when entering the function you'll get the value of the model from before the push. So if you click and it's checked, the value will still be false (hasn't been updated yet). So either use !$scope.thirtyDay or place in a $timeout.

EDIT: As Mike correctly mentioned in his comment, you would probably be better of using ng-change instead of ng-click (this way the other property won't trigger as well when interacting with the other one). I would also join the recommendation and suggest considering a different function for different properties, but I'm not sure exactly to which use you're doing this.

Sign up to request clarification or add additional context in comments.

4 Comments

Your $timeout comment highlights an issue with his implementation. It would be better to use ng-change over ng-click.
@MikeRobinson I agree. Personally I probably would have used two different functions as well.
@OmriAharon: Thank you for the help. Could you explain a bit further what do you mean by a different function for different properties
Yes, I imagine each checkbox does something else when triggered, so maybe you want to invoke a different function for each click, and not call changeAxis for each one. But that totally depends on what you're implementing.
1

Here is working demo

HTML

<div ng-app="myApp">
<div ng-controller="myCtrl" >
 <div class="checkbox">
        <label>
        <input type="checkbox" ng-model="thirtyDay" ng-change="changeAxis1()">
        Last 30 Days
        </label>
         </div>
         <div class="checkbox">
        <label>
        <input type="checkbox" ng-model="wholeTimeline" ng-change="changeAxis2()">
        Whole Timeline
        </label>
         </div>
</div>
</div>

JS

var app = angular.module('myApp', []);
app.controller('myCtrl',['$scope', function($scope) {        
    $scope.changeAxis1 = function() {                   
            if ($scope.thirtyDay) {
                    alert("checked 30");
                    $scope.wholeTimeline = false;
                }                
    };   
     $scope.changeAxis2 = function() {                  
            if($scope.wholeTimeline) {
                    alert("checked whole");
                    $scope.thirtyDay = false;
                }           
    };   
}]);

2 Comments

Thanks is there a way to uncheck the other one while one is checked?
Yes, Just define two function and update the model values. I have updated the demo. Please check.
1

You need to add controller to myApp with this line.

myApp.controller('MyCtrl', MyCtrl);

You need to link changeAxis function to scope

$scope.changeAxis = changeAxis;

So your app.js will be something like

var myApp = angular.module('myApp',[]);
myApp.controller('MyCtrl', MyCtrl);

function MyCtrl($scope) {
   function changeAxis(){
          if ($scope.thirtyDay) {
                    alert("checked 30");
                }
          else if($scope.wholeTimeline) {
                    alert("checked whole");
                }

        };
        $scope.changeAxis = changeAxis;
}

I hope you have added ng-app to your html. Also please consider the ngChange suggestion mentioned in the other answer.

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.