I have recently started learning AngularJS and I am trying to send a message from one controller to another. I have looked at various examples and my code seems to be similar, but it is not working. Why is $rootScope.$on not working? Can anyone help me with this?
HTML:
<div ng-controller="Ctrl">
{{message}}
<div ng-controller="Ctrl1">
<p>Ctrl1</p>
{{test}}
</div>
</div>
Ctrl:
angular
.module("app")
.controller("Ctrl",["$rootScope","$scope",Ctrl]);
function Ctrl($rootScope,$scope){
var test = "Bla bla bla!";
$scope.message = test;
$rootScope.$broadcast('aaa', test);
}
Ctrl1:
angular
.module("app")
.controller("Ctrl1",["$rootScope","$scope", Ctrl1]);
function Ctrl1($rootScope, $scope){
$rootScope.$on('aaa', function(event, args){
console.log("This message doesn't appear!");
$scope.test=args;
});
}