I'm currently working on an application that has been highly modularized. Within this app, there is a factory responsible for sending notifications to a controller from another module. To accomplish this, I am utilizing the $rootScope
object to trigger an event called newNotification
and pass along the necessary data.
Everything is functioning as expected with the code provided below:
notifications.factory.js
(function() {
'use strict';
function notificationsFactory($rootScope) {
return {
sendNotification: function(data) {
switch(data.type) {
case 'actors':
$rootScope.$broadcast('newNotification', data);
break;
.....
}
}
}
};
angular.module('features.notifications.factory', [])
.factory('notificationsFactory', ['$rootScope', notificationsFactory]);
})();
However, I would like to create a cleaner factory by returning an object with named functions since more functions will be added in the future. Essentially, I want it to look something like this:
notifications.factory.js
(function() {
'use strict';
function notificationsFactory($rootScope) {
return {
sendNotification: sendNotification
}
};
function sendNotification(data) {
switch(data.type) {
case 'actors':
$rootScope.$broadcast('newNotification', data);
break;
....
}
};
angular.module('features.notifications.factory', [])
.factory('notificationsFactory', ['$rootScope', notificationsFactory]);
})();
The issue arises when the $rootScope
object is not defined within the sendNotification
function, which is understandable. I am currently struggling to find a solution to this problem.
I have searched online for a potential solution, but expressing the complexity of the issue without incorporating some code makes it challenging. Hence, my post requesting assistance.
Thank you for your help and understanding :)