Let's say I have a DIV
with a function specified to be called when clicked on it by ng-click
.
Inside this DIV
there's a BUTTON
which also have a function to be called when clicked on, specified by ng-click
.
When I click the button, both the DIV
's click and the BUTTON
's click functions are called.
How do I make that only the BUTTON
's function is called?
I have set up this fiddle for better illustrate what I mean. Below is the code:
HTML:
<body ng-app="Test">
<section ng-controller="TestCtrl as ctrl">
<div class="square" ng-click="ctrl.divClick()">
<span>My text</span>
<button ng-click="ctrl.buttonClick()" >My button</button>
</div>
</section>
</body>
JavaScript:
(function() {
var app = angular.module('Test', []);
app.controller('TestCtrl', [function() {
this.divClick = function() {
alert('div clicked');
};
this.buttonClick = function() {
alert('button clicked');
}
}]);
})();
EDIT:
As suggested by akonsu, I just need to stop the event propagation. This worked for me. Here's an updated Fiddle showing how it works.