首页 > 代码库 > angular 控制器之间值得传递

angular 控制器之间值得传递

<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>




app.controller(‘SelfCtrl‘, function($scope) {  $scope.click = function () {    $scope.$broadcast(‘to-child‘, ‘child‘);    $scope.$emit(‘to-parent‘, ‘parent‘);  }});app.controller(‘ParentCtrl‘, function($scope) {  $scope.$on(‘to-parent‘, function(event,data) {    console.log(‘ParentCtrl‘, data);	   //父级能得到值  });  $scope.$on(‘to-child‘, function(event,data) {    console.log(‘ParentCtrl‘, data);	   //子级得不到值  });});app.controller(‘ChildCtrl‘, function($scope){  $scope.$on(‘to-child‘, function(event,data) {    console.log(‘ChildCtrl‘, data);		 //子级能得到值  });  $scope.$on(‘to-parent‘, function(event,data) {    console.log(‘ChildCtrl‘, data);		 //父级得不到值  });});app.controller(‘BroCtrl‘, function($scope){    $scope.$on(‘to-parent‘, function(event,data) {      console.log(‘BroCtrl‘, data);		  //平级得不到值    });    $scope.$on(‘to-child‘, function(event,data) {      console.log(‘BroCtrl‘, data);		  //平级得不到值    });  });

angular 控制器之间值得传递