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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:10:35  来源:igfitidea点击:

Pass object in function with ng-click

javascriptjqueryangularjsangularjs-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 collapseQuestioni want to pass object that will be reffered to clicked li, but when i send it like collapseQuestion(this);i get some Objectbut 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 中获取原始元素