简体   繁体   English

ng-click和子选择器

[英]ng-click and child selectors

I have an owl carousel that doesn't fire events on the cloned items. 我有一个不会在克隆项目上触发事件的猫头鹰传送带。 So, as solution people propose to move the event from direct target to its parent one: 因此, 当人们提出解决方案时,建议将事件从直接目标移到其父目标:

from: 从:

$('#multiple-carousel .item').click(function () {
        alert("click");
});

to: 至:

$('#multiple-carousel').on('click', '.item', function () {
    alert("click");
});

Could I similarly change from my code: 我可以从我的代码中类似地更改:

<div id="multiple-carousel">
   <div class="item" ng-click="myClick($event)">...</div>
</div>

to something like: 像这样:

<div id="multiple-carousel" ng-click[???".item"???]="myClick($event)??">
   <div class="item" >...</div>
</div>

The second parameter in jquery's .on is the filter which descendants will trigger the event, however, events in AngularJS are usually handled via directives ( ng-click in this case ) which do not support the same functionality as jQuery does. jquery的.on中的第二个参数是过滤器,后代将触发事件,但是,AngularJS中的事件通常通过指令(在这种情况下为ng-click)处理,该指令不支持与jQuery相同的功能。

You can add click event with angular.element , however if you want to use directive ( which is the right thing to do ), you have to do something like this: 您可以使用angular.element添加click事件,但是,如果要使用指令(这是正确的做法),则必须执行以下操作:

$scope.myClick = function(event) {
    if (event.target.className === 'item') {
    // do smth
    }
}

See this answer to see the difference between event.currentTarget and event.target . 请参阅答案以查看event.currentTarget和event.target之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM