Angular的$broadcast,$emit and $on
来源:互联网 发布:淘宝没给发票 编辑:程序博客网 时间:2024/05/22 14:07
发送事件可以用
向子控制器传递信息 $scope.$emit('name', 'args');
或者是
向父控制器传递信息 $scope.$broadcast('name', 'args');
name:事件的名字
args:事件所需要传递的参数
接受事件可以用(仅此一个方法可以接受事件)
接受来自于子或父传来的信息$scope.$on('name',function(event,data){//从$emit或者$broadcast中获取的args事件传递来的信息})
例子如下
注意
bodyCtrl为indexCtrl的父
indexChildCtrl为indexCtrl的子
<!DOCTYPE html><html ng-app="wsscat"> <head> <meta charset="UTF-8"> <title></title> </head> <script type="text/javascript" src="js/angular.js"></script> <body ng-controller="bodyCtrl"> <article ng-controller="indexCtrl"> {{name}} <button ng-click="click()">Ok</button> <section ng-controller="indexChildCtrl"> <input ng-model="name" ng-change="changName()" /> {{name}} </section> </article> <article ng-controller="indexBrotherCtrl"> {{name}} </section> </article> </body> <script> var app = angular.module('wsscat', []); app.controller('bodyCtrl', function($scope) { $scope.$on('to-parent', function(event, data) { console.log('I am the parent, I got it', data); }); }) app.controller('indexCtrl', function($scope) { $scope.name = "wsscat"; $scope.click = function() { //向下广播事件 $scope.$broadcast('to-child', 'haha'); //向上广播事件 $scope.$emit('to-parent', 'hehe'); } //子控制器indexChildCtrl改变名字后,向上发送to-parent-name事件委托告诉indexCtrl,$scope.name值已经发生改变,并一起作出改变 $scope.$on('to-parent-name', function(event, data) { console.log('I am the parent to-parent-name, I got it', data); $scope.name = data; }); }) app.controller('indexChildCtrl', function($scope) { $scope.$on('to-child', function(event, data) { console.log('I am the child, I got it', data); }); $scope.changName = function() { $scope.$emit('to-parent-name', $scope.name); } }) app.controller('indexBrotherCtrl', function($scope,$rootScope) { $rootScope.$on('to-parent', function(event, data) { console.log('I am the parent from $rootScope, I got it', data); $scope.name = data; }); }) app.service("hiEventService", function($rootScope) { this.broadcast = function() { $rootScope.$broadcast("hi") } this.listen = function(callback) { $rootScope.$on("hi", callback) } }) </script></html>
上面的结果,当我们按下按钮,indexCtrl分别向它的父于子进行消息传递,打印信息如下
0 0
- Angular的$broadcast,$emit and $on
- angular $on $emit $broadcast
- angular ¥on $emit $broadcast 的使用
- angular中的$on、$emit、$broadcast
- angularjs的事件 $broadcast and $emit and $on
- angularjs的事件 $broadcast and $emit and $on
- angularjs的事件 $broadcast and $emit and $on
- angularjs的事件 $broadcast and $emit and $on
- angularjs的事件 $broadcast and $emit and $on
- angularjs的$on、$emit、$broadcast
- AngularJS的$on、$emit、$broadcast
- angularjs的$on、$emit、$broadcast
- angualr的$emit,$broadcast,$on
- Angular中$broadcast和$emit的使用方法
- Angular $emit $broadcast $on controller的理解以及使用中出现的问题
- AngularJS的 $on、$emit和$broadcast使用
- AngularJS $on、$emit和$broadcast的使用
- $on、$emit和$broadcast的使用
- 使用Markdown编辑器写博客
- Docker-Windows 2008 R2安装使用
- Nand Flash基础知识与坏块管理机制的研究
- js学习笔记:引用类型——Array
- 链表中与环相关的问题
- Angular的$broadcast,$emit and $on
- isEmpty和isBlank区别
- js简单学-主计时器
- Android ListView的OnItemClickListener()参数详解
- [电影]《指环王》新老三部曲完全赏析(魔戒再现)
- ajax VS websocket
- ibatis 中like使用
- 查找二叉树的最小深度
- 操作系统原理——进程描述和控制