Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add many functions in ONE ng-click?

I've be looking for how to execute this but I can't find anything related so far, :( I could nest both functions yes but I'm just wondering if this is possible? I'd like to do this literally:

<td><button class="btn" ng-click="edit($index) open()">Open me!</button></td> 

My JS code at the moment:

$scope.open = function () {   $scope.shouldBeOpen = true; };        $scope.edit = function(index){    var content_1, content_2;       content_1 = $scope.people[index].name;       content_2 = $scope.people[index].age;    console.log(content_1); }; 

I'd like to call two functions with just one click, how can I do this in angularJS? I thought it'd be straight forward like in CSS when you add multiple classes...but it's not :(

like image 841
YoniGeek Avatar asked May 29 '13 12:05

YoniGeek


People also ask

Can Onclick have multiple functions?

Yes, you can call two JS Function on one onClick. Use semicolon (';') between both the functions.

How do I add multiple functions to an Eventlistener?

We can invoke multiple functions on a single event listener without overwriting each other. To do this we simply call the addEventListener() method more than once with a different function. In the example above, we add another event listener for the same event on the same button.

Can you call two functions on click Angular?

-> on click of a button you need to check data from two functions with (click) attribute. Do you : A) create a third function which encapsulates the two & pass that in the (click).


2 Answers

You have 2 options :

  1. Create a third method that wrap both methods. Advantage here is that you put less logic in your template.

  2. Otherwise if you want to add 2 calls in ng-click you can add ';' after edit($index) like this

    ng-click="edit($index); open()"

See here : http://jsfiddle.net/laguiz/ehTy6/

like image 108
Maxence Avatar answered Oct 02 '22 16:10

Maxence


You can call multiple functions with ';'

ng-click="edit($index); open()" 
like image 45
amit Avatar answered Oct 02 '22 15:10

amit