AngularJS 브로드캐스트 이벤트

1631 단어 angularjs


	
		
		
		<script type="text/javascript" src="js/angular.min.js"/>
		<script type="text/javascript">
			var app = angular.module("my_app", []);
			app.controller('div_child1', function($scope) {
				$scope.my_broadcast = function() {
					var data = {
						controller_name: 'div_child1',
						msg: ' '
					}
					// $emit parent controller event data
					// $broadcast child controller event data
					$scope.$emit("from_child1_to_main", data);
					$scope.$broadcast("from_child1_to_grandchild1", data);
				}
			});
			app.controller('div_main', function($scope) {
				//$on (div_child1) 
				$scope.$on("from_child1_to_main", function(event, data) {
					console.log('div_main');
					console.log(JSON.stringify(data));
				});
			});
			app.controller('div_grandchild1', function($scope) {
				//$on (div_child1) 
				$scope.$on("from_child1_to_grandchild1", function(event, data) {
					console.log('div_grandchild1');
					console.log(JSON.stringify(data));
				});
			});
		</script>
	
	
		<div ng-controller="div_main">
			<div ng-controller="div_child1">
				<button ng-click="my_broadcast();"> </button>
				<div ng-controller="div_grandchild1">
				</div>
			</div>
		</div>
	

</code></pre> 
  <p> </p> 
 </div> 
</div>
                            </div>
                        </div>

좋은 웹페이지 즐겨찾기