angular同一个页面多个controller值传递

时间:2022-03-17 22:26:28
<div ng-controller="ParentCtrl">                  //父级  
<div ng-controller="SelfCtrl"> //自己
<a ng-click="click()">click me</a>
<div ng-controller="ChildCtrl"></div> //子级
</div>
<div ng-controller="BroCtrl"></div> //平级
</div>
phonecatControllers.controller('SelfCtrl', function($scope) {      $scope.click = function () {          $scope.$broadcast('to-child', 'child');          $scope.$emit('to-parent', 'parent');      }  });    phonecatControllers.controller('ParentCtrl', function($scope) {      $scope.$on('to-parent', function(d,data) {          console.log(data);         //父级能得到值      });      $scope.$on('to-child', function(d,data) {          console.log(data);         //子级得不到值      });  });    phonecatControllers.controller('ChildCtrl', function($scope){      $scope.$on('to-child', function(d,data) {          console.log(data);         //子级能得到值      });      $scope.$on('to-parent', function(d,data) {          console.log(data);         //父级得不到值      });  });    phonecatControllers.controller('BroCtrl', function($scope){      $scope.$on('to-parent', function(d,data) {          console.log(data);        //平级得不到值      });      $scope.$on('to-child', function(d,data) {          console.log(data);        //平级得不到值      });  }); 


用$broadcast赋的值,只能子级能得到值,父级得不到,用$emit赋的值,只能父级得到值,子级得不到。平级都得不到值