在 angularjs 中获取 $this [英] Get the $this in angularjs

查看:32
本文介绍了在 angularjs 中获取 $this的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆