簡體   English   中英

導入角度JSON對象

[英]import angular JSON 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' }
                    ],

它用於在單個頁面上填充一些列表。 但是,現在我需要在其他頁面上重復使用它。 我可以將其復制到適當的頁面,但是我寧願對其進行重構,以便兩個控制器都在同一列表中工作,以實現更好的可維護性。 我一直試圖在'MainApp'模塊上將其聲明為常量,工廠和/或指令,但沒有成功。 我想念什么?

使用工廠來解決此問題。 更改頁面時,數據可以保留在工廠和服務中。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM