<!doctype html> <html ng-app="app"> <head> <meta charset="utf-8"> <script src="./angular-1.5.8/angular.js"></script> </head> <body> <div ng-controller="ParentCtrl"> <div ng-controller="SelfCtrl"> <a ng-click="click($event)">click me</a> <div ng-controller="ChildCtrl"></div> </div> <div ng-controller="BroCtrl"></div> </div> </body> </html> <script> var phonecatControllers = angular.module('app',[]); phonecatControllers.controller('SelfCtrl', function($scope) { $scope.click = function () { $scope.$broadcast('to-child', 'child'); $scope.$emit('to-parent', 'parent'); } $scope.$on('to-parent', function(event,data) { console.log('SelfCtrl to-parent', data); //父級能獲得值 }); $scope.$on('to-child', function(event,data) { console.log('SelfCtrl to-child', data); //父級能獲得值 }); }); phonecatControllers.controller('ParentCtrl', function($scope) { $scope.$on('to-parent', function(event,data) { console.log('ParentCtrl to-parent', data); //父級能獲得值 }); $scope.$on('to-child', function(event,data) { console.log('ParentCtrl to-child',data); //子級得不到值 }); }); phonecatControllers.controller('ChildCtrl', function($scope){ $scope.$on('to-child', function(event,data) { console.log('ChildCtrl to-child',data); //子級能獲得值 }); $scope.$on('to-parent', function(event,data) { console.log('ChildCtrl to-parent',data); //父級得不到值 }); }); phonecatControllers.controller('BroCtrl', function($scope){ $scope.$on('to-parent', function(event,data) { console.log('BroCtrl to-parent',data); //平級得不到值 }); $scope.$on('to-child', function(event,data) { console.log('BroCtrl to-child',data); //平級得不到值 }); }); </script>
Output:html
SelfCtrl to-child childapp
ChildCtrl to-child childcode
SelfCtrl to-parent parenthtm
ParentCtrl to-parent parentip