我正在學習angularJS廣播和發射。下面的代碼似乎不起作用,即FirstCtrl和SecondCtrl不接收廣播消息。任何Angularjs廣播和查詢
<div ng-controller="FirstCtrl">
<div ng-controller="SecondCtrl"> </div>
</div>
var app = angular.module("myapp", []);
function FirstCtrl($scope){
this.name = "xxxx";
this.age = 31;
$scope.$on("message", function(e, opt){
console.log("received message First");
});
}
function SecondCtrl($scope){
this.name = "yyyy";
this.age = 31;
$scope.$on("message", function(e, opt){
console.log("received message second");
});
}
app.controller("FirstCtrl", FirstCtrl);
app.controller("SecondCtrl", SecondCtrl);
app.run(function($rootScope){
$rootScope.$on("message", function(e,opt){
console.log("root receive message");
});
$rootScope.$broadcast("message", { message : "root broadcast"});
});