I am using $rootScope.$emit() for raising events from service to controller.
It is working fine when emitting from the instance of service that is referenced in controller but $rootScope.$on is not getting called when emitting from the instance of service I got from angular injector in plain javascript .
Following is my code
Plain JavaScript
var msgHandlerJS = function () {
var injector = angular.injector(['ng', 'services']);
var aMsgHandlerService = injector.get('msgHandlerService');
aMsgHandlerService.TestScopeWatch();
}
Calling the above when dom is ready :
angular.element(document).ready(function () {
msgHandlerJS();
});
Service
(function (module) {
var msgHandlerService = function ($rootScope, $http, $q, $timeout) {
var TestScopeWatch = function () {
$http.get('~/test.json').then(function (result) {
EmailPacket = result.data.Packet;
$rootScope.$emit("EmailPacketChanged", EmailPacket);
});
};
return {
//making public
TestScopeWatch:TestScopeWatch,
};
};
module.factory("msgHandlerService", ["$rootScope","$http", "$q","$timeout", msgHandlerService]);
}(angular.module("services")));
Controller
(function (module) {
function testController($rootScope,$scope, msgHandlerService) {
$rootScope.$on("EmailPacketChanged", function(event,data){
alert('Here I am');
};
};
module.controller("testController", ["$rootScope","$scope", "msgHandlerService", testController]);
}(angular.module('app')));