javascript 使用 ng-click 在函数中传递对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30438879/
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
Pass object in function with ng-click
提问by demo
I have template which starts like :
我有这样开头的模板:
<li class="list-group-item" ng-repeat="question in question.ChildQuestions" collapse-toggler ng-click="collapseQuestion(this);"> ...
In collapseQuestion
i want to pass object that will be reffered to clicked li
, but when i send it like collapseQuestion(this);
i get some Object
but it seems like it isn't li
(i can't get any class of that object to check what exactly it is).
在collapseQuestion
我想传递将被点击的对象li
,但是当我发送它时就像collapseQuestion(this);
我得到了一些Object
但它似乎不是li
(我无法获得该对象的任何类来检查它到底是什么)。
So what is correct way to pass object in ng-click
?
那么传递对象的正确方法是什么ng-click
?
采纳答案by Ashley Medway
You need to parse the event itself.
您需要解析事件本身。
ng-click="collapseQuestion($event);"
Then in the function use $event.currentTarget
然后在函数中使用 $event.currentTarget
function collapseQuestion($event) {
$event.currentTarget //do something with currentTarget
}
This post maybe of use: get original element from ng-click
这篇文章可能有用:从 ng-click 中获取原始元素
回答by garec
ng-click="collapseQuestion(question)"
see How I pass an object as a parameter in a ng-click within a ng-repeat? AngularJS