Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple argument to ng-click method

Tags:

In my code i like to pass to arguments to the function specified inside the ng-click attribute.

<div class="shout" ng-repeat="user in users">  <p>{{user.name}}</p>  <img src="media/images/delete.png" ng-click="deleteUser({{$index}},{{user._id}})"/> </div> 

and in the controller

function deleteUser(index, userId){...} 

the parameter index is to remove the user from $scope.user and user._id to remove it from the mongodb. i am a newbee to angular js.

when i tried like this the deleteUser is not getting called. if i pass single argument it works like charm but when i pass more than its not working

like image 447
Jaison Justus Avatar asked Jan 30 '13 14:01

Jaison Justus


People also ask

How to pass 2 parameters with onClick?

If you want to call a function when the onclick event happens, you'll just want the function name plus the parameters. Then if your parameters are a variable (which they look like they are), then you won't want quotes around them.

What is the difference between click and Ng-click?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.


1 Answers

You don't need {{ }} while specifying arguments to an event handlers (ng-click). The correct syntax would be ng-click="deleteUser($index, user._id):

<div class="shout" ng-repeat="user in users">  <p>{{user.name}}</p>  <img src="media/images/delete.png" ng-click="deleteUser($index, user._id)"/> </div> 

Here is a working plunker based on the code you've provided (check the console to see that click handler is working correctly): http://plnkr.co/edit/26A4Rj0FScPXYU7z92E6?p=preview

like image 105
pkozlowski.opensource Avatar answered Sep 18 '22 14:09

pkozlowski.opensource