angular 컨트롤러 간 전달

5256 단어 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); //        }); });

좋은 웹페이지 즐겨찾기