The following is an example of the event system in angular, with $emit (), $on (), $broadcast (). As follows
In the following code, the controller as syntax is used, the use of this syntax, the benefit or the original directly in the controller to bind the view object directly to the $scope object above the difference,
You can check out one of my previous posts.
Direct Sticker Code
<! DOCTYPE html>
(function () {' Use strict '; Angular.module (' app ', []). Controller (' AccountController ', function ($scope) {var vm = this; vm.accountbalance = 0; Vm.activate = _activate; function _activate () {$scope. $on ("amountdeposited", _amountdepositedhandler); $scope. $on (' Amountwithdrawn ', _amountwithdrawnhandler); } function _amountdepositedhandler (event, args) {vm.accountbalance + = eval (args.amount); } function _amountwithdrawnhandler (event, args) {if (Vm.accountbalance-eval (Args.amount) < 0) {$scope. $broadcast ("withdrawalnotallowed", {balance:vm.accountBalance}); } else {vm.accountbalance-= eval (args.amount); }} _activate (); }). controller (' Depositcontroller ', function ($scope) {var vm = this; Vm.amount = 0; Vm.deposit = _deposit; $scope. Name = ' YSR '; function _deposit () {alert (this.name); $scope. $emit ("amountdeposited", {amount:vm.amount}); Vm.amount = 0; } console.log (this); }). controller (' Withdrawcontroller ', function ($scope) {var vm = this; Vm.amount = 0; Vm.validationerror = ""; Vm.activate = _activate; Vm.withdraw = _withdraw; function _activate () {$scope. $on ("withdrawalnotallowed", _withdrawalnotallowedhandler); } function _withdraw () {vm.validationerror = ""; $scope. $emit ("Amountwithdrawn", {amount:vm.amount}); Vm.amount = 0; } function _withdrawalnotallowedhandler (event, args) {Vm.validationerror = "You cannot withdraw More than $ "+ args.balance; } _activate (); });}) ();/* (function () {' Use strict '; Angular. Module (' app ', []). Controller (' AccountController ', AccountController). Controller (' Depositco Ntroller ', Depositcontroller). Controller (' Withdrawcontroller ', withdrawcontroller); AccountController. $inject = [' $scope ']; function AccountController ($scope) {var vm = this; vm.accountbalance = 0; Vm.activate = _activate; function _activate () {$scope. $on ("amountdeposited", _amountdepositedhandler); $scope. $on ("Amountwithdrawn", _amountwithdrawnhandler); } function _amountdepositedhandler (event, args) {vm.accountbalance + = eval (args.amount); } function _amountwithdrawnhandler (event, args) {if (Vm.accountbalance-eval (Args.amount) < 0) { $scope. $broadcast ("withdrawalnotallowed", {balance:vm.accountBalance}); } else {VM.ACCOuntbalance-= eval (args.amount); }} _activate (); } depositcontroller. $inject = [' $scope ']; function Depositcontroller ($scope) {var vm = this; Vm.amount = 0; Vm.deposit = _deposit; function _deposit () {$scope. $emit ("amountdeposited", {amount:vm.amount}); Vm.amount = 0; }} withdrawcontroller. $inject = [' $scope ']; function Withdrawcontroller ($scope) {var vm = this; Vm.amount = 0; Vm.validationerror = ""; Vm.activate = _activate; Vm.withdraw = _withdraw; function _activate () {$scope. $on ("withdrawalnotallowed", _withdrawalnotallowedhandler); } function _withdraw () {vm.validationerror = ""; $scope. $emit ("Amountwithdrawn", {amount:vm.amount}); Vm.amount = 0; } function _withdrawalnotallowedhandler (event, args) {Vm.validationerror = "You cannot withdraw more th AN $ "+ args.balance; } _activate (); }})();*/
Reference: Http://www.ezzylearning.com/tutorial/angularjs-event-notification-system-broadcast-emit-and-on-functions
AngularJS series (vi)---$emit (), $on (), $broadcast () use