Reputation: 103
I have an angular 1.x controller with the following object:
notificationTypes: [ { NotificationType: '*', NotificationTitle: 'All notifications' },
{ NotificationType: '0', NotificationLabel: 'System Alert', NotificationTitle: 'System alerts' },
{ NotificationType: '1', NotificationLabel: 'Pending Task', NotificationTitle: 'Pending tasks' },
{ NotificationType: '2', NotificationLabel: 'Update', NotificationTitle: 'Updates' },
{ NotificationType: '3', NotificationLabel: 'Missed Message', NotificationTitle: 'Missed messages' }
],
It is used to populate a few lists on a single page. However, I am now in a situation where I need to reuse it on other pages. I could just copy it over to the appropriate pages, but I'd rather refactor it so both controllers work from the same list for better maintainability. I've been trying to declare it on the 'MainApp' module as a constant, factory, and/or directive without success. What am I missing?
Upvotes: 0
Views: 66
Reputation: 198
Use a factory to solve this problem. Data can persist in factories and services when changing pages.
angular.module('App', [...])
.factory('NotificationFactory', function () {
var notificationTypes: [ { NotificationType: '*', NotificationTitle: 'All notifications' },
{ NotificationType: '0', NotificationLabel: 'System Alert', NotificationTitle: 'System alerts' },
{ NotificationType: '1', NotificationLabel: 'Pending Task', NotificationTitle: 'Pending tasks' },
{ NotificationType: '2', NotificationLabel: 'Update', NotificationTitle: 'Updates' },
{ NotificationType: '3', NotificationLabel: 'Missed Message', NotificationTitle: 'Missed messages' }
];
return notificationTypes;
})
Upvotes: 1