angularjsHow can I use AngularJS to prevent default behavior?
AngularJS provides a few different ways to prevent default behavior.
- Using
ng-click
Directive:
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
$event
Object:
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 create an editable AngularJS application?
- How do I use Angular to zip files?
- How can I prevent XSS attacks when using AngularJS?
- How do I use the window.open function with AngularJS?
- How can I use AngularJS to watch for changes in my data?
- How do I integrate an Angular Yandex Map into my software development project?
- How do I use an AngularJS variable in a template?
- How do I use an AngularJS template?
- How can I create an AngularJS quiz?
- How do I open a link in a new tab using AngularJS?
See more codes...