We're going to use rootScope emit here to send out events and then we're going to listen for them in the run block. We're going to use rootScope on down in the run block to listen for the same event that we sent out to the system.

angular
.module('app', [])
.controller('MainCtrl', function($scope, Order) {
$scope.newOrder = function() { new Order(); };
})
.factory('Order', function($emit) {
function Order() {
this.email = 'brett.shollenberger@gmail.com';
this.product = 'Macbook Pro';
$emit('order:created', this);
}
return Order;
})
.factory('$emit', function($rootScope) {
return function() { $rootScope.$emit.apply($rootScope, arguments); };
})
.factory('Email', function($window) {
function Email(text) {
$window.alert(text);
}
return Email;
})
.run(function($rootScope, Email) {
$rootScope.$on('order:created', function(event, order) {
new Email('Email sent to ' + order.email + ' for ' + order.product);
});
});

We don't want ot inject $rootscope into Order factry, so we use mediator to create a $emit() function, and each time we create a new order, it will send the create event to run block. isnide the run block we send the email. This is good because, the order doesn't know the Email event, it onlys emits a create event.

最新文章

  1. 【Tips】史上最全H1B问题合辑——保持H1B身份终级篇
  2. golang的内置类型map的一些事
  3. HTTPS那些事(二)SSL证书(转载)
  4. dedecms在列表或首页取得文章首图的功能改进
  5. poj 3254 Corn Fields
  6. nodejs的第四天学习笔记
  7. 【SPI】Polling Interrupt DMA
  8. NSString类的相关用法
  9. js原型继承与多态 How to apply virtual function in javascript
  10. [Bug]The maximum array length quota (16384) has been exceeded while reading XML data.
  11. Unity3d截图保存到Android相册的实现
  12. OpenCV2学习笔记04:图像的读取与显示
  13. C#仪器数据文件解析-PDF文件
  14. web自动化框架如何设计
  15. Python机器学习笔记 K-近邻算法
  16. TensorFlow实战Google深度学习框架10-12章学习笔记
  17. react native 环境报错
  18. Kali安装虚拟机遇到的问题
  19. 【python】参数中的*args和**kwargs
  20. 原生的js轮播图

热门文章

  1. sql语句分页代码
  2. UICollectionView出现the behavior of the UICollectionViewFlowLayout is not defined because:
  3. 如何做好Flex与Java交互
  4. jQuery实现checkbox全选反选及删除等操作
  5. 谈.Net委托与线程——解决窗体假死
  6. document.all和jq trigger原理
  7. VISUAL STUDIO 2005连接MYSQL数据库
  8. spring的beans.xml的配置
  9. poj 3026Borg Maze
  10. pojPots