$ rootScope. $ emit doesn't work
I am trying to pass an event in AngularJS to a second controller using:
First controller:
$scope.checkThemeContent = function(theme) {
console.log("Trying to get cards for theme id: " +theme.id);
console.log(theme);
$scope.selectedTheme = theme;
if(theme.count_of_cards >0) {
//$scope.$emit('detailDisplayed', {});
$scope.displayedTemplate = 'detail';
$rootScope.$emit('detailDisplayed', {});
} else {
$scope.displayedTemplate = 'empty';
}
};
Second controller:
$rootScope.$on('detailDisplayed', function(event, args) {
alert('Received');
$scope.initDataGrid();
});
But the event doesn't fire.
I've tried scoping backwards and it works.
Where could the problem be?
I followed this tutorial:
http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/
Thanks for any help.
+3
source to share
2 answers
Remember: the issuance goes up the prototype chain, the broadcast goes down. If you are already at the top (i.e. $rootScope
), it cannot be higher. Use $broadcast
to trigger an event across the board.
$rootScope.$broadcast('detailDisplayed');
Check out this article:
https://blog.safaribooksonline.com/2014/04/08/refactoring-angularjs-get-hands-filthy/
+3
source to share