码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-12-18 18:20:40      阅读:159      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!