按下按钮时如何在函数中传递值

时间:2015-08-06 10:49:50

标签: angularjs

当我点击编辑我时!按钮,它在Controller中调用名称为mySwitch的函数。现在我也想要id和它的名字,意味着我如何在mySwitch函数中获得特定的id和名称?

<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>

当我按下编辑我时,此函数调用!按钮

$scope.mySwitch = function() {

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

2 个答案:

答案 0 :(得分:3)

函数必须来自html mySwitch(),您可以传递任意数量的参数,例如:mySwitch(param1, param2, param3, etc)。因此,要获取用户ID和名称,您应该将按钮更改为:

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

控制器:

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

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

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

答案 1 :(得分:2)

更改ng-click功能以接受参数。

HTML:

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

角控制器:

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