I am trying to "stopPropagation" to prevent a Twitter Bootstrap navbar dropdown from closing when an element (link) inside an li is clicked. Using this method seems to be the common solution.
In Angular, seems like a directive is the place to do this? So I have:
// do not close dropdown on click directives.directive('stopPropagation', function () { return { link:function (elm) { $(elm).click(function (event) { event.stopPropagation(); }); } }; });
... but the method does not belong to element:
TypeError: Object [object Object] has no method 'stopPropagation'
I tie in the directive with
<li ng-repeat="foo in bar"> <div> {{foo.text}}<a stop-propagation ng-click="doThing($index)">clickme</a> </div> </li>
Any suggestions?
The stopPropagation() method prevents propagation of the same event from being called. Propagation means bubbling up to parent elements or capturing down to child elements.
The event. stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed. Tip: Use the event. isPropagationStopped() method to check whether this method was called for the event.
In addition to all the built-in AngularJS directives, you can create your own directives. New directives are created by using the . directive function. To invoke the new directive, make an HTML element with the same tag name as the new directive.
preventDefault() will not allow the user to leave the page and open the URL. The event. stopPropagation() method stops the propagation of an event from occurring in the bubbling or capturing phase.
I've used this way: Created a directive:
.directive('stopEvent', function () { return { restrict: 'A', link: function (scope, element, attr) { if(attr && attr.stopEvent) element.bind(attr.stopEvent, function (e) { e.stopPropagation(); }); } }; });
that could be used this way:
<a ng-click='expression' stop-event='click'>
This is more generic way of stopping propagation of any kind of events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With