我正在研究用AngularJS編寫的電子應用程序。在Electron應用程序的主流程中,我正在監聽DownloadItem事件。因此,每次用戶下載文件時,都會觸發事件。在我的Main.js中,我有以下代碼。 在自己的控制器中的角度手柄事件
win.webContents.session.on('will-download', (event, item, webContents) => {
item.once('done', (event, state) => {
if (state === 'completed') {
win.webContents.send('download-message', 'completed', item.getSavePath());
} else if (state === 'interrupted') {
win.webContents.send('download-message', 'failed');
}
})
});
上的東西,我在等,在「主」控制器這樣的事件(代表應用程序的起點)的角邊。所以,在這裏我有一個看起來像這樣的代碼:
const { ipcRenderer } = require('electron');
ipcRenderer.on('download-message', function (event, method, filepath) {
if (method === 'completed') {
// Do something interesting
} else if (method === 'failed') {
// Do something interesting
}
});
這只是正常,但感覺那種醜陋來處理所有的主要的應用程序控制器,這些事件。我想將它分離到它自己的Controller(例如downloadController)。
我想過要在它自己的服務(例如downloadsService)中監聽這個事件,但是因爲我想操作應用程序的視圖(例如顯示一些成功/失敗的消息),我不認爲這會是這個功能的正確位置。
所以,我正在尋找一種很好的方式將此功能分離到特定的控制器。
也許我只是不太瞭解Angular及其控制器的概念,所以任何建議都會非常有幫助。
在此先感謝!
謝謝。在問之前應該已經通過了文檔。 – Founded1898