angularjsHow can I use AngularJS to prevent default behavior?
AngularJS provides a few different ways to prevent default behavior.
- Using
ng-clickDirective:
ng-click directive allows us to prevent default behavior of an element. We can use $event.preventDefault() to prevent the default behavior.
For example:
<a href="www.example.com" ng-click="$event.preventDefault()">Link</a>
- Using
$eventObject:
We can also use $event object to prevent default behavior of an element. We can use $event.preventDefault() to prevent the default behavior.
For example:
<a href="www.example.com" ng-click="doSomething($event)">Link</a>
<script>
$scope.doSomething = function($event) {
$event.preventDefault();
}
</script>
- Using
$event.stopPropagation():
$event.stopPropagation() can be used to prevent event bubbling.
For example:
<div ng-click="doSomething($event)">
<a href="www.example.com" ng-click="$event.stopPropagation()">Link</a>
</div>
<script>
$scope.doSomething = function($event) {
console.log('This will not be called');
}
</script>
Helpful links
More of Angularjs
- How can I use AngularJS to transform XLTS files?
- How do I use AngularJS to watch for changes in a variable?
- How can I use AngularJS to construct an XSS payload?
- How do I use the window.open function with AngularJS?
- How do I create a link in AngularJS?
- How can I add a PDF viewer to my AngularJS application?
- How can I use AngularJS to watch for changes in my data?
- How do I use the AngularJS Wiki to find information about software development?
- How can I use AngularJS UI Router to create an application with multiple views?
- How do I use AngularJS to select an item from a list?
See more codes...