在 angularjs 中获取 $this

编程入门 行业动态 更新时间:2024-10-25 14:33:56
本文介绍了在 angularjs 中获取 $this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

这是我的问题.

HTML 结构:

<tr><td><a ng-click=aClick()>Click Me</a></td></tr>

我不能有任何与 和 相关联的 id/class

I cannot have any id/class associated with the and

我需要的是,在单击单击我"时,<tr> 被隐藏.我需要一个 jQuery 解决方案.一些我无法使用 $(this) 的原因.

What I require is that on the click of 'Click Me', the <tr> gets hidden. I need a jQuery solution. Some how I am not able to use $(this).

功能:

$scope.aClick = function() {
   $(this).parent().parent().css('display','block');
};

但是这个语句给了我一个错误.

But this statement gives me an error.

推荐答案

注意:我不建议在控制器中使用 dom 操作,您可以编写指令来执行此操作.也就是说,您可以使用 $event 来获取事件对象,您可以从中获取事件目标并将其与 jquery 一起使用.

Note: I wouldn't recommend using dom manipulation in a controller, you can write a directive to do this. That said you can use the $event to get the event object, from which you can get the event target and use it with jquery.

<tr><td><a ng-click="aClick($event)">Click Me</a></td></tr>

  $scope.aClick = function(event) {
     $(event.target).parent().parent().css('display','none');
  };

演示:Plunker

更新
更合适的角度解决方案是使用 ng-hide

Update
A more appropriate angular solution will be is to use ng-hide

<tr ng-hide="hideRow"><td><a ng-click="hideRow = true">Click Me</a></td></tr>

演示:Plunker

使用 ng-repeat 更新演示

这篇关于在 angularjs 中获取 $this的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 00:33:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1011366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:angularjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!