首页 > Web开发 > 详细

angularjs的事件 $broadcast and $emit and $on

时间:2015-12-18 18:18:04      阅读:164      评论:0      收藏:0      [点我收藏+]

angularjs的事件 $broadcast and $emit and $on

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body ng-app="search">
    <div ng-controller="ParentCtrl">
        <div ng-controller="SelfCtrl">
            <a class="btn" ng-click="click()">click me</a>
            <div ng-controller="ChildCtrl"></div>
        </div>
        <div ng-controller="BroCtrl"></div>
    </div>
    <script src="angular.min.js"></script>
    <script>
        var search = angular.module(‘search‘, [])
        .controller(‘ParentCtrl‘, function($scope) {
            $scope.$on(‘to-child‘, function(e, d) {
                console.log(‘关我毛事‘)
            })
             $scope.$on(‘to-parent‘, function(e, d) {
                console.log(‘we are the parent, I got it‘, d)
             })
        })
        .controller(‘SelfCtrl‘, function($scope) {
            $scope.click = function () {
                $scope.$broadcast(‘to-child‘, ‘haha‘) // 向子
                $scope.$emit(‘to-parent‘, ‘hehe‘) // 向父
            }
        })
        .controller(‘ChildCtrl‘, function($scope){
            $scope.$on(‘to-child‘, function(e, d) {
                console.log(‘I\‘ the child, I got it‘, d)
            })
            $scope.$on(‘to-parent‘, function(e, d) {
                console.log(‘关我毛事‘)
            })
        })
        .controller(‘BroCtrl‘, function($scope){
            $scope.$on(‘to-child‘, function(e, d) {
                console.log(‘关我毛事‘)
            })
            $scope.$on(‘to-parent‘, function(e, d) {
                console.log(‘关我毛事‘)
            })
        })
    </script>
</body>
</html>

 

angularjs的事件 $broadcast and $emit and $on

原文:http://www.cnblogs.com/jzm17173/p/5057691.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!