We ' re going to use Rootscope emit this send out events and then we ' re going to listen for them in the run block. We ' re going to use Rootscope on and the run block to listen for the same event, the we sent out to the system.
angular. Module (' App ', []). Controller (' Mainctrl ',function($scope, Order) {$scope. Neworder=function() {NewOrder ();}; }). Factory (' Order ',function($emit) {functionOrder () { This. email = ' [email protected] '; This. Product = ' Macbook Pro '; $emit (' Order:created ', This); } returnOrder; }). Factory (' $emit ',function($rootScope) {return function() {$rootScope. $emit. Apply ($rootScope, arguments);}; }). Factory (' Email ',function($window) {functionEmail (text) {$window. alert (text); } returnEmail; }). Run (function($rootScope, Email) {$rootScope. $on (' Order:created ',function(event, order) {NewEmail (' 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 CR Eate a new order, it'll 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.
[AngularJS] Design pattern:simple Mediator