Javascript 将多个参数传递给 ng-click 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14605926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:22:25  来源:igfitidea点击:

Passing multiple argument to ng-click method

javascriptangularjs

提问by Jaison Justus

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

在我的代码中,我喜欢将参数传递给 ng-click 属性中指定的函数。

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

参数index是从$scope.user和user._id中移除用户,将其从mongodb中移除。我是 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

当我这样尝试时,没有调用 deleteUser。如果我传递单个参数,它就像魅力一样,但是当我传递的不仅仅是它不起作用时

回答by pkozlowski.opensource

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

{{ }}在为事件处理程序 ( ng-click)指定参数时不需要。正确的语法是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

这是一个基于您提供的代码的工作 plunker(检查控制台以查看点击处理程序是否正常工作):http://plnkr.co/edit/26A4Rj0FScPXYU7z92E6?p=preview