1.$scope.$on

view事件

//View被加载但是DOM树构建之前时: 

$scope.$on('$viewContentLoading', function(event, viewConfig){ ... }); 

//View被加载而且DOM树构建完成时: 

$scope.$on('$viewContentLoaded', function(event){ ... }); 

//路由路径发生改变时 current:要到达的路径  previous:上一个路径

$scope.$on('$locationChangeStart', function (event, current, previous) {  });

//销毁事件

$scope.$on('$destroy', function () {    });

ng-route路由有几个常用的事件:

$routeChangeStart:这个事件会在路由跳转前触发

$routeChangeSuccess:这个事件在路由跳转成功后触发

$routeChangeError:这个事件在路由跳转失败后触发

$scope.$on("$routeChangeStart",function(event, current, previous){  });

页面刷新:$route.reload();

 ui-router

使用angular来做项目时,习惯性的使用第三方路由插件ui-router配置路由。每一个状态都对应着一个页面,因此对路由状态改变的监听也变的十分重要。可以使用:$rootScope.$on(…….)监听

$stateChangeStart: 表示状态切换开始

$stateNoFound:没有发现

$stateChangeSuccess:切换成功

$stateChangeError:切换失败

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ ... })

回调函数的参数解释:event:当前事件的信息,toState:目的路由状态,toParams:传到目的路由的参数,fromState:起始路由状态,toParams:起始路由的参数;

2.父子controller传值

子级传递数据给父级  
// 子级传递
$scope.checkLoggedIn = function(type) {
$scope.transferType = type;
$scope.$emit('transfer.type', type);
} // 父级接收
$scope.$on('transfer.type', function(event, data) {
$scope.transferType = data;
});
$scope.checkLoggedIn = function() {
var type = $scope.transferType;
}

父级传递数据给子级

// 父级传递
$scope.transferType = '';
$scope.checkLoggedIn = function(type) {
$scope.transferType = type;
$scope.$broadcast('transfer.type', type);
} // 子级接收
$scope.transferType = '';
$scope.$on('transfer.type', function(event, data) {
$scope.transferType = data;
});
$scope.checkLoggedIn = function() {
var type = $scope.transferType;
}

angular之$on、$emit、$broadcast的更多相关文章

  1. Angular页面传参的四种方法

    1. 基于ui-router的页面跳转传参 (1)在Angular的app.js中用ui-route定义路由,比如有两个页面, 一个页面(producers.html)放置了多个producers,点 ...

  2. 迷你MVVM框架 avalonjs 入门教程

    新官网 请不要无视这里,这里都是链接,可以点的 OniUI组件库 学习教程 视频教程: 地址1 地址2 关于AvalonJs 开始的例子 扫描 视图模型 数据模型 绑定 作用域绑定(ms-contro ...

  3. angularjs项目的页面跳转如何实现

    链接:https://www.zhihu.com/question/33565135/answer/696515Angular页面传参有多种办法,根据不同用例,我举5种最常见的:PS: 在实际项目中, ...

  4. AngularJS中页面传参方法

    1.基于ui-router的页面跳转传参 (1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的 ...

  5. Angular $scope和$rootScope事件机制之$emit、$broadcast和$on

    Angular按照发布/订阅模式设计了其事件系统,使用时需要“发布”事件,并在适当的位置“订阅”或“退订”事件,就像邮箱里面大量的订阅邮件一样,当我们不需要时就可以将其退订了.具体到开发中,对应着$s ...

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

    angularjs的事件 $broadcast and $emit and $on <!DOCTYPE html> <html> <head> <meta c ...

  7. Angular中Controller之间的信息传递(第二种办法):$emit,$broadcast,$on

    $emit只能向parent controller传递event与data( $emit(name, args) ) $broadcast只能向child controller传递event与data ...

  8. Angular $broadcast和$emit和$ond实现父子通信

    <!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta ...

  9. angular之$broadcast、$emit、$on传值

    文件层级 index.html <!DOCTYPE html> <html ng-app="nickApp"> <head> <meta ...

随机推荐

  1. 2.ajax+servlet实现注册时用户名验证

    效果: 精灵图(来源:从百度注册中download下来的): userVerify.jsp <%@ page language="java" contentType=&quo ...

  2. dynamics crm 365 附件上传图片并且显示。

    参考了几篇博客做的: 新增websource文件(html): <!DOCTYPE html> <html> <head> <title>注释</ ...

  3. python自动化day4-函数嵌套、名称空间、作用域、装饰器

    1.函数嵌套 #函数的镶套调用:在调用一个函数的过程中,又调用其他函数 def max2(x,y): if x > y: return x else: return y def max4(a,b ...

  4. mongodb常用语法

    // Employee表 { "_id" : "9e794fb9-12dc-457c-8c5a-69fe45c57685", "No" : ...

  5. 持续集成、持续交付、持续部署、Jkens、git

    一  持续集成.持续交付.持续部署 1. 持续集成 持续集成指的是,频繁地(一天多次)将代码集成到主干.持续集成的目的,就是让产品可以快速迭代,同时还能 保持高质量.它的核心措施是,代码集成到主干之前 ...

  6. lua table、ipairs/pairs基础知识

    1.什么是table? table是Lua最复杂最强大的数据结构,Lua本身并不是面向对象语言,但是对面向对象中毒比较深的程序员,可以借助table”完美”地模拟面向对象编程.最简单地,我们可以把ta ...

  7. CentOS系统服务配置资源限制ulimit

    使用Systemd管理服务的,均可使用此方法. 在 /usr/lib/systemd/system/xxx.service中,添加如下内容即可: [Service] # Other directive ...

  8. 如何应用前端技术唤起app及判断用户来源及与原生交互的原理

    做唤起时需要native端进行配合, h5唤起app这种需求是常见的.在移动为王的时代,h5在app导流上发挥着重要的作用. 目前我们采用的唤起方式是url scheme(iOS,Android平台都 ...

  9. Oracle 行列转换总结

    行列转换包括以下六种情况:*列转行*行转列*多列转换成字符串*多行转换成字符串*字符串转换成多列*字符串转换成多行 下面分别进行举例介绍. 首先声明一点,有些例子需要如下10g及以后才有的知识:a.掌 ...

  10. 酷炫字体背景图的实现——神奇的background-clip: text

    愉快的时光总是飞快,七天小长假已接近尾声,抓住假期的尾巴,再学个新知识点——css的background-clip: text属性...会不会有种陌生的感觉,毕竟在我们的印象里,background- ...