serge
serge

Reputation: 15239

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>

Upvotes: 0

Views: 126

Answers (1)

tehabstract
tehabstract

Reputation: 549

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.

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:

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

See this answer to see the difference between event.currentTarget and event.target .

Upvotes: 2

Related Questions