脚本宝典收集整理的这篇文章主要介绍了angular events,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<!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', function(event,data) {
console.log('SelfCtrl to-child', data); //父级能得到值
});
});
phonecatControllers.controller('ParentCtrl', function($scope) {
$scope.$on('to-parent', function(event,data) {
console.log('ParentCtrl to-parent', data); //父级能得到值
});
$scope.$on('to-child', function(event,data) {
console.log('ParentCtrl to-child',data); //子级得不到值
});
});
phonecatControllers.controller('ChildCtrl', function($scope){
$scope.$on('to-child', function(event,data) {
console.log('ChildCtrl to-child',data); //子级能得到值
});
$scope.$on('to-parent', function(event,data) {
console.log('ChildCtrl to-parent',data); //父级得不到值
});
});
phonecatControllers.controller('BroCtrl', function($scope){
$scope.$on('to-parent', function(event,data) {
console.log('BroCtrl to-parent',data); //平级得不到值
});
$scope.$on('to-child', function(event,data) {
console.log('BroCtrl to-child',data); //平级得不到值
});
});
</script>
Output:
SelfCtrl to-child child
ChildCtrl to-child child
SelfCtrl to-parent parent
ParentCtrl to-parent parent
以上是脚本宝典为你收集整理的angular events全部内容,希望文章能够帮你解决angular events所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。