38

Is there a way I can get ng-click to call two functions?

I would like something along the lines of

ng-click ="{search(),match()}" 

Instead of how I have it now, which is:

ng-click = "search()"
sudo bangbang
  • 27,127
  • 11
  • 75
  • 77
user2516917
  • 383
  • 1
  • 3
  • 5
  • 1
    possible duplicate of [How to add many functions in ONE ng-click?](http://stackoverflow.com/questions/16813945/how-to-add-many-functions-in-one-ng-click) – AJ Meyghani May 15 '14 at 20:32

4 Answers4

86

You can call multiple functions with ';'

ng-click="search(); match()"
daCoda
  • 3,583
  • 5
  • 33
  • 38
  • 2
    are you sure? That doesn't seem to work for me EDIT: Nvm, its actually ng-click="search();match()" Thanks for leading me to the right answer! – user2516917 Jul 11 '13 at 03:26
  • You can do it, however this is not best practice. e.g if you are passing events through, something can kill the second function so it will never execute. This way is possible but not best practice. – Harry May 11 '16 at 11:50
1

Please use search() one function and add a callback function of match() inside search() function;

ng-click = "search()";
Sanjib Debnath
  • 3,556
  • 2
  • 22
  • 16
0

using the ng-click to call function:

ng-click="search()"

using the callback approach if function search () depends on the value returned by function math () in the controller do:

$scope.match() = function(cb){

//do something and return value
        var x = 10;
        x = x+10;

cb(x)

};

$scope.search() = function(){

   //call math function 
   $scope.match(function(value){

   myCalc = value;

   });
};
krekto
  • 1,403
  • 14
  • 18
0

You can use multiple methods with (click) separated by ;

(click)="search();match()"
ifnotak
  • 4,147
  • 3
  • 22
  • 36
Flash
  • 155
  • 2
  • 2