$('ul').on('click', 'li', function(){ // ....
html <ul bn-delegate="li a | selectFriend( friend )"> <li ng-repeat="friend in friends"> <!-- Delegate target. --> <a href="#">{{ friend.name }}</a> <!-- Delegate target. --> </li> </ul>
//js element.on( "click.bnDelegate", selector, function( event ) { // Prevent the default behavior - this is // not a "real" link. event.preventDefault(); // Find the scope most local to the target // of the click event. var localScope = $( event.target ).scope(); // Invoke the expression in the local scope // context to make sure we adhere to the // proper scope chain prototypal inheritance. localScope.$apply( function() { expressionHandler( localScope ); } ); } );
- Simplifies initialization and saves memory: no need to hang a lot of handlers.
- Less code: when adding and removing items, you do not need to install or remove handlers.
- Convenience changes: you can massively add or remove elements by changing the innerHTML.
- First, the event should float. Most of the events pop up, but not all.
- Secondly, the delegation creates an additional load on the browser, because the handler is started when the event occurs anywhere in the container, not necessarily on the elements that interest us.
But usually this load is small and is not a problem.
Source: https://habr.com/ru/post/218813/