Vivek Gondliya
Vivek Gondliya

Reputation: 308

How to pass value in a function when it press button

When i click on Edit Me! button,it calls function which name is mySwitch in Controller.Now i also want id AND name with it,means how can i get particular id and name in mySwitch function?

<tbody>
<tr dir-paginate="user in users|orderBy:sortKey:reverse|filter:search|itemsPerPage:5">

        <td>{{user.id}}</td>                            
        <td>{{user.name}}</td>
        <td><button ng-click="mySwitch()">Edit Me!</button></td>
    </tr>
</tbody>

This function call when I press Edit Me! Button

$scope.mySwitch = function() {

      // Here I want id and name....
       //When i press Edit Me! button This function is called...
}; 

Upvotes: 0

Views: 40

Answers (2)

Razvan B.
Razvan B.

Reputation: 6771

A function has to be from the html as mySwitch() and you can pass it any number of parameters, like: mySwitch(param1, param2, param3, etc). So, to get the user id and name, you should change your button to:

<button ng-click="mySwitch(user.id, user.name)">Edit Me!</button>

Controller:

$scope.mySwitch = function(id, name) {
    console.log('ID: ', id)
    console.log('Name: ', name)
}; 

or

<button ng-click="mySwitch(user)">Edit Me!</button>

$scope.mySwitch = function(user) {
    console.log('ID: ', user.id)
    console.log('Name: ', user.name)
}; 

Upvotes: 3

cverb
cverb

Reputation: 643

Change your ng-click function to accept parameters.

HTML:

<button ng-click="mySwitch(user.id, user.name)">Edit Me!</button>

Angular-controller:

$scope.mySwitch = function(id, name) {
  // Here you have id and name....
  }; 

Upvotes: 2

Related Questions