zoukankan      html  css  js  c++  java
  • $on、$emit、$broadcast用法

    <!DOCTYPE html>
    <html ng-app="firstApp">
    
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
                div {
                    border: 1px solid #ff7300;
                    padding: 20px;
                    margin: 10px;
                    border-radius: 5px;
                }
            </style>
            <script src="js/angular1.5.11/angular.js"></script>
        </head>
    
        <body>
            <div ng-controller="ParentController">
                <!--父级-->
                <div ng-click="clkP()">click--给child</div>
                <div ng-controller="OneSelfController">
                    <!--自己-->
                    <span ng-click="clkme()">click me</span>
                    <div ng-controller="ChildController">
                        它是OneSelfController的子级
                    </div>
                    <!--子级-->
                </div>
                <div ng-controller="siblingsController">
                    它与OneSelfController是平级
                </div>
                <!--平级-->
            </div>
    
            <script>
                var app = angular.module('firstApp', []); //app模块名
                app.controller('OneSelfController', function($scope) {
                    $scope.clkme = function() {
                        $scope.$broadcast('sendChild', '我给子控制器传递数据');
                        $scope.$emit('sendParent', '冒泡到父元素')
                    }
                })
                .controller('ParentController', function($scope) {
                    $scope.$on('sendParent', function(event, data) { //监听在子控制器中定义的 sendParent 事件
                        console.log('OneSelfController传过来的', data, event.name, event); //事件名称:sendParent
                    });
                    $scope.clkP = function() {
                        $scope.$broadcast('sendAllChild', '让siblingsController接收到');
                    }
    
                })
                .controller('ChildController', function($scope) {
                    $scope.$on('sendChild', function(event, data) { //监听在子控制器中定义的 sendChild 事件
                        console.log('ChildCtrl', data, event.name, event); // 事件名称:sendChild
                    });
                })
                .controller('siblingsController', function($scope) {
                    $scope.$on('sendAllChild', function(event, data) {
                        console.log('值过来吧', data);
                    });
                    //下面事件不会触发
                    $scope.$on('sendParent', function(event, data) {
                        console.log('平级得不到值', data);
                    });
                    $scope.$on('sendChild', function(event, data) {
                        console.log('平级得不到值', data);
                    });
                });
            </script>
        </body>
    
    </html>
  • 相关阅读:
    单例模式
    leetcode:Minimum Subarray
    leetcode:Minimum Path Sum
    内存分配以及优化
    完整性检查
    类型定义之可选的一些笔记
    CSS|CSS module
    TS 操作符
    React|虚拟 Dom、render函数、shouldComponentUpdate
    React|常用相关框架
  • 原文地址:https://www.cnblogs.com/smallzhu/p/8124903.html
Copyright © 2011-2022 走看看