angular五大服务顺序,angularJS $事件处理程序的触发顺序

I was wondering two things, in the context of angularJS event handling.

How is defined the order in which handlers listening to the same event are triggered?

Is it a sign of a bad design if you start wondering about this?

After reading documentation on angular $on, $broadcast and $emit as well as native DOM event flow I think I understand in which order event handlers will be trigger in different scopes. The problem is when several handlers listen in the same scope ($rootScope for example) from various places (Controllers vs Services for example).

To illustrate the problem I have put together a jsfiddle with one controller and two services, all communicating through $rootScope http://jsfiddle.net/Z84tX/

Thanks

解决方案

Very good question.

Event handlers are executed in order of initialization.

I haven't really thought about this before, because my handlers never needed to know which one run first, but by the look of you fiddle I can see that the handlers are called in the same order in which they are initialized.

In you fiddle you have a controller controllerA which depends on two services, ServiceA and ServiceB:

myModule

.controller('ControllerA',

[

'$scope',

'$rootScope',

'ServiceA',

'ServiceB',

function($scope, $rootScope, ServiceA, ServiceB) {...}

]

);

Both services and the controller define an event listener.

Now, all dependencies need to be resolved before being injected, which means that both services will be initialized before being injected into the controller. Thus, handlers defined in the services will be called first, because service factories are initialized before controller.

Then, you may also observe that the services are initialized in order they are injected. So ServiceA is initialized before ServiceB because they are injected in that order into the controller. If you changed their order inside the controller signature you'll see that their initilization order is also changed (ServiceB comes before ServiceA).

So, after the services are initialized, the controller gets initialized as well, and with it, the event handler defined within.

So, the end result is, on $broadcast, the handlers will be executed in this order: ServiceA handler, ServiceB handler, ControllerA handler.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360690.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

地形 凹陷

地形凹下去:先要设置terrain-flattenheightmap 这个 转载于:https://www.cnblogs.com/wolfsky6/archive/2013/05/29/3106078.html

REST API的演变

每个开发人员都以某种方式接触到API 。 要么为一家大公司集成一个主要系统,或者使用最新的图形库生成一些精美的图表,要么直接与他喜欢的编程语言进行交互。 事实是,API无处不在! 它们实际上代表了当今Internet的基本构建块&#…

python爬携程酒店评论_python爬虫爬取携程网的酒店评论数据时,有个请求参数不知道是怎么生成的?...

是下面这个代码生成的,看不懂:,_getElevenValue:function(e){function o(e){for(var o["A","B","C","D","E","F","G","H","I","J","K"…

Oracle MAF中的LOV

我们都喜欢最强大的ADF功能值列表之一。 使用它们,我们可以在ADF应用程序中声明性地轻松构建非常复杂的功能。 一件好事是,我们在Oracle MAF中也有类似的方法。 在ADF BC中,我们在业务服务级别(基本上在实体或VO级别)定…

怎么移动矩形选框工具选中的东西_ps矩形选框工具怎么用,你值得一看的技巧...

PS是一款非常好用的图片制作软件,我们可以使用矩形选框工具,选择自己需要的区域进行操作,下面小编就教大家ps矩形选框工具怎么用,希望可以帮助到大家。操作方法01首先我们打开PS进入到主界面,如图所示。02之后我们需要…

functionclass[LeetCode]Path Sum II

在本篇文章中,我们主要介绍functionclass的内容,自我感觉有个不错的建议和大家分享下 每日一道理 只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获。只有追求,才会品味堂堂…

Java8中的Mapreduce

Wordcount旨在Mapreduce其余编程世界中的“ Hello world”。 最近,我一直在探索一些更著名的Java 8功能,例如Lambda,Streams和Optionals,所以我认为在Java中简化Wordcount版本会很不错。 Java的Stream和Lambda函数使实现数据处理…

stream 过滤俩个字段_Java8 Stream:2万字20个实例,玩转集合的筛选、归约、分组、聚合...

点波关注不迷路,一键三连好运连连!先贴上几个案例,水平高超的同学可以挑战一下:从员工集合中筛选出salary大于8000的员工,并放置到新的集合里。统计员工的最高薪资、平均薪资、薪资之和。将员工按薪资从高到低排序&…

C# 栈的实现

早前写得栈的实现&#xff0c;基本功能都有。 代码&#xff1a; /// <summary>/// 栈/// </summary>public class Stack{private object[] data; //用data数组来储存数据private int size; //栈的大小private int top; //top指针public object this[int…

一个JSF清单示例

这是使用JSF 2.0&#xff08;JavaServer Faces&#xff09;构建的示例列表应用程序。 该应用程序是待办事项列表。 该应用程序具有添加&#xff0c;编辑或删除列表中项目的功能。 待办事项具有名称和描述属性。 完成的应用程序的JSF页面具有&#xff1a; 使用h:selectOneList…

ubuntu下的jdk进行升级_如何在ubuntu上更新jdk版本

{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云数据库专家保驾护航&#xff0c;为用户…

hdu 2444(二分图的判断以及求最大匹配)

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2444思路&#xff1a;首先要判断能否构成二分图&#xff0c;用bfs对当前点u染色&#xff0c;对u的邻接点v的颜色进行判断&#xff0c;如果为染色&#xff0c;则染色后入队列&#xff0c;否则&#xff0c;判断co…

Delta架构:统一Lambda架构并利用Hadoop / REST中的Storm

最近&#xff0c;一群人要求我详细介绍我为我们的书《分布式实时计算的风暴蓝图》撰写的Druid / Storm集成。 德鲁伊很棒。 风暴很大。 两者一起解决了实时维查询/聚合问题。 实际上&#xff0c;人们正在将其视为主流&#xff0c;将其称为RAD Stack &#xff0c;并添加了“ La…

activiti mysql 版本_Mysql8.0.17版本不能自动创建activiti表的坑

maven项目如下&#xff1a;配置好数据库&#xff0c;和activiti的配置之后&#xff0c;开始执行流程部署package com.yuanqiao.first_activiti.deployment;import java.util.List;import org.activiti.engine.ProcessEngine;import org.activiti.engine.ProcessEngines;import …

Linux Kernel 多个本地信息泄露漏洞

漏洞名称&#xff1a;Linux Kernel 多个本地信息泄露漏洞CNNVD编号&#xff1a;CNNVD-201306-028发布时间&#xff1a;2013-06-04更新时间&#xff1a;2013-06-04危害等级&#xff1a; 漏洞类型&#xff1a; 威胁类型&#xff1a;本地CVE编号&#xff1a; 漏洞来源&#xff1a…

JDBC教程– ULTIMATE指南(PDF下载)

在本文中&#xff0c;我们提供了全面的JDBC教程&#xff08;Java数据库连接性&#xff09;&#xff0c;这是Oracle提供的一种API&#xff0c;允许程序员处理Java应用程序中的不同数据库&#xff1a;它允许开发人员建立与数据库的连接&#xff0c;定义特定的客户端如何访问给定的…

Spinner 学习笔记

Adapter 适配器//方法一&#xff1a;直接使用字符串数组//String[] sSexList new String[]{"男", "女"};//方法二&#xff1a;使用资源文件String[] sSexList getResources().getStringArray(R.array.Sexarray);//实例化一个集合适配器ArrayAdapter<S…

Java 8发布一年后,IDE和编译器尚未完全就绪

一年前&#xff0c;即2014年3月18日 &#xff0c;发布了Java SE 8&#xff0c;并伴随着通过lambda表达式和streams API实现了功能性编程的幸福。 这对于我们所有的Java生态系统都是个好消息&#xff0c;许多人已经升级到Java8。Stack Overflow已经提出了大约2500个有关Java 8的…

mysql增量_mysql实时增量备份

采用binlog日志的好处掌控所有更改操作,必要时可用于恢复数据数据库主从复制的必要条件[linyouyilocalhost~]# vim /etc/my.cnf[mysqld].. ..log-binmysql-bin //启用二进制日志&#xff0c;并指定前缀.. ..[linyouyidbsvr1 ~]# service mysqld restart确认binlog日志文件新启用…

使用JDK 8流在包装对象的集合和包装对象的集合之间转换

我发现使用基于Java的应用程序时&#xff0c; 装饰器和适配器有时会很有用。 这些“包装器”在各种情况下都能很好地工作&#xff0c;并且相当容易理解和实现&#xff0c;但是当需要包装对象的层次结构而不是单个对象时&#xff0c;事情可能会变得有些棘手。 在这篇博客文章中&…