加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular events

发布时间:2020-12-17 09:21:42 所属栏目:安全 来源:网络整理
导读:!doctype htmlhtml ng-app="app" head meta charset="utf-8" script src="./angular-1.5.8/angular.js"/script /head body div ng-controller="ParentCtrl" div ng-controller="SelfCtrl" a ng-click="click($event)"click me/a div ng-controller="ChildCtr
<!doctype html>
<html ng-app="app">
    <head>
    <meta charset="utf-8">
        <script src="./angular-1.5.8/angular.js"></script>
    </head>
    <body>
        <div ng-controller="ParentCtrl">                 
            <div ng-controller="SelfCtrl">                
                <a ng-click="click($event)">click me</a>
                <div ng-controller="ChildCtrl"></div>     
            </div>
            <div ng-controller="BroCtrl"></div>           
        </div>
    </body>
</html>
<script>
var phonecatControllers = angular.module('app',[]);
phonecatControllers.controller('SelfCtrl',function($scope) {
    $scope.click = function () {
        $scope.$broadcast('to-child','child');
        $scope.$emit('to-parent','parent');
    }
    $scope.$on('to-parent',function(event,data) {
        console.log('SelfCtrl to-parent',data);         //父级能得到值
    });
    $scope.$on('to-child',data) {
        console.log('SelfCtrl to-child',data);         //父级能得到值
    });
});
phonecatControllers.controller('ParentCtrl',function($scope) {
    $scope.$on('to-parent',data) {
        console.log('ParentCtrl to-parent',data) {
        console.log('ParentCtrl to-child',data);         //子级得不到值
    });
});
phonecatControllers.controller('ChildCtrl',function($scope){
    $scope.$on('to-child',data) {
        console.log('ChildCtrl to-child',data);         //子级能得到值
    });
    $scope.$on('to-parent',data) {
        console.log('ChildCtrl to-parent',data);         //父级得不到值
    });
});
phonecatControllers.controller('BroCtrl',function($scope){
    $scope.$on('to-parent',data) {
        console.log('BroCtrl to-parent',data);        //平级得不到值
    });
    $scope.$on('to-child',data) {
        console.log('BroCtrl to-child',data);        //平级得不到值
    });
});
</script>

Output:

  1. SelfCtrl to-child child

  2. ChildCtrl to-child child

  3. SelfCtrl to-parent parent

  4. ParentCtrl to-parent parent

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读