0

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?

2
  • 1
    Use an angular service or factory to share data across various parts of the app Commented Dec 8, 2017 at 18:50
  • If it doesn´t change you could also export it as a constant. Otherwise you should follow ,@charlierfl ´s advice Commented Dec 8, 2017 at 18:56

1 Answer 1

1

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;
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.