angularjs 事件向上向下传播

<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="angular.min.js"></script>
<script>

var m1 = angular.module('myApp',[]);
m1.controller('Aaa',['$scope',function($scope){
    $scope.count = 0;
    $scope.$on('myEvent',function(event){//$on('myEvent')绑定myEvent事件,
        $scope.count++;
        console.log( event.targetScope == event.currentScope );
        console.log(event.name);
        event.stopPropagation();
    });
}]);
</script>
</head>

<body>
<div ng-controller="Aaa">
    {{count}}
    <div ng-controller="Aaa" ng-click="$emit('myEvent')"> //向上广播myEvent事件,则上面的count会加1,$broadcast('myEvent')是向下广播事件,下面的count会加1,ng-click="count=count+1"只有这个count加1,上下的count不加1.
        {{count}}
        <div ng-controller="Aaa">
            {{count}}
        </div>
    </div>
</div>
</body>
</html>

 

posted @ 2016-08-07 23:22  无天666  阅读(1238)  评论(0编辑  收藏  举报