从ng-click获取jQuery元素引用

时间:2014-08-06 23:34:26

标签: angularjs

我有一个半长度任意长度的ng-repeat,带有"标签沿"根据某些条件呈现的元素:

<div ng-repeat-start="day in [1,2,3,4,5,6,7] track by $index">my repeat</div>
<div ng-repeat-end ng-if="shouldRender(day)" class="maybe">maybe rendered</div>

我想要做的是在重复元素上设置click事件并引用NEXT SIBLING&#34;可能会渲染&#34;元素为了做一些jQuery魔术。所以我想要做的是添加ng-click="getNextMaybe(this)"并处理它以找到下一个兄弟.maybe元素。所以现在我有:

<div ng-repeat-start="day in [1,2,3,4,5,6,7] track by $index" ng-click="getNextMaybe(this)">my repeat</div>
<div ng-repeat-end ng-if="shouldRender(day)" class="maybe">maybe rendered</div>

function myCtrl($scope){
  $scope.getNextMaybe = function(repeatElem){
    var nextMaybe = $(repeatElem).next('.maybe');
  }
}

我认为我对此实例中this的含义以及如何将其转换为jQuery对象进行处理感到困惑。

谢谢!

1 个答案:

答案 0 :(得分:9)

您可以将$event传递给您的函数并查看$event.target以获取元素(plnkr):

<button ng-click="onClick($event)">Button</button>

控制器:

$scope.onClick = function(evt) {
  console.dir(evt.target);
}