user1943020
user1943020

Reputation:

How can I do more than one action with an ng-click in AngularJS

In my code there is:

<button class="btn float-right" data-ng-click="test()">
    Reset
</button>

Is it possible for me to fire more than one function when I do a click of the button. If so how should I code that?

Update. I would like to call the test() function in the current scope and the otherTest() function in the parent scope / controller.

Upvotes: 1

Views: 2393

Answers (2)

Khanh TO
Khanh TO

Reputation: 48982

I would like to call the test() function in the current scope and the otherTest() function in the parent scope / controller.

Just define your ortherTest in your parent scope. For example:

function ParentController($scope){
     $scope.otherTest = function(){

     }
}

There are 2 ways to achieve what you want: You could try:

function CurrentController($scope){
     $scope.test = function(){

     }
}

data-ng-click="test();otherTest()"

Or:

function CurrentController($scope){
     $scope.test = function(){
          $scope.otherTest();
          //Your test code for this function
     }
}

data-ng-click="test()"

As scope is inherited, your current scope will inherit the otherTest from the parent scope

Upvotes: 1

gion_13
gion_13

Reputation: 41533

You should modify your test function to incorporate more logic:

$scope.test = function(){
    test1();
    test2();
    //...
    testN();
} 

Upvotes: 3

Related Questions