[英]import angular JSON object
I have an angular 1.x controller with the following object: 我有一个带有以下对象的angular 1.x控制器:
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.
我一直试图在'MainApp'模块上将其声明为常量,工厂和/或指令,但没有成功。 What am I missing?
我想念什么?
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;
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.