angularjs $broadcast event fired|triggered twice
https://github.com/angular/angular.js/issues?q=is%3Aissue+ngClick+event+fired+twice+
by Guillaume Seguin
HTML
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script src="https://code.angularjs.org/1.3.8/angular-animate.min.js"></script>
<script src="https://code.angularjs.org/1.3.8/angular-sanitize.min.js"></script>
<script src="http://mgcrea.github.io/angular-strap/dist/angular-strap.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.2/angular-strap.tpl.min.js"></script>
<div ng-controller="Main">
<a ng-repeat="link in links" href="" ng-click="link.click()">{{ link.title }}</a>
<a href="" ng-click="click()">Click me!</a>
<div ng-controller="SubMain"></div>
</div>
JavaScript
var app = angular.module('app', ['ngAnimate', 'ngSanitize', 'mgcrea.ngStrap']);
app.controller('Main', function ($rootScope, $scope, Links, $modal) {
$scope.$on( 'launch', function (event, param) {
alert('launch' + param);
console.log('launch', param);
});
$scope.links = Links;
var myModal = $modal({title: 'My Title', content: 'My Content', show: false});
$scope.click = function(){
myModal.$promise.then(myModal.toggle());
$rootScope.$broadcast('fire', 'clicked');
}
});
app.controller('SubMain', function ($scope) {
$scope.$on( 'fire', function (event, param) {
alert('fire' + param);
console.log('fire', param);
});
});
app.factory('Links', function( $rootScope, $modal ){
return[
{
title: 'Click me!',
modal : $modal({title: 'My Title', content: 'My Content', show: false}),
click: function(){
this.modal.$promise.then(this.modal.toggle());
$rootScope.$broadcast('fire', 'clicked');
$rootScope.$broadcast('launch', 'touched');
}
},
{
title: 'Click meee!',
modal : $modal({title: 'My Title', content: 'My Content', show: false}),
click: function(){
console.log( this.title );
this.modal.$promise.then(this.modal.toggle());
$rootScope.$broadcast('fire', 'clicked');
$rootScope.$broadcast('launch', 'touched');
}
},
{
title: 'don\'t click me!',
click: function(){
console.log( this.title );
}
}
];
});