[英]Angular 2 view not updating after receiving ipcRenderer.on from Electron
我正在使用 ipcRenderer 從我的 main.js 中的瀏覽器對話框中檢索文件夾路徑。
但由於某種原因,它不會更新我視圖中的文本字符串。
我知道為了讓它工作,我需要做一個 setTimeout 來更新(感謝谷歌!)。 但由於某種原因,這不起作用。
我使用的代碼如下。
import { Component, OnInit } from '@angular/core';
declare var electron: any;
@Component({
selector: 'my-app',
template:
//added (click)="debug()" in order to update the {{path}} manually
`<h1 (click)="debug()">My First Angular 2 App with Electron</h1>
<br />Project Path: {{[(path)]}}
<button (click)="btnClick()">Select project path</button>
<br /> <br />`
})
export class AppComponent implements OnInit {
ipc:any;
path:string;
constructor(){
this.path = __dirname;
}
ngOnInit(){
electron.ipcRenderer.on('project-path-reply', (event, arg) => {
setTimeout(() => {
this.path = arg.return[0];
console.log('updated')
})
})
}
btnClick(){
electron.ipcRenderer.send('project-path',1);
}
debug(){
console.log(this.path);
}
}
有人能指出我正確的方向嗎這是我第一個使用電子的應用程序。
親切的問候,
泰沃
對於這種情況,請使用NgZone
import { Component,NgZone } from '@angular/core';
現在,在NgZone
提供的.run()
函數中更新您的應用程序
constructor(zone:NgZone) {
ipcRenderer.on('project-path-reply', (event, arg) => {
this.zone.run(()=>{
// the code that requires change detection here, just like below //
this.path = arg.return[0];
console.log('updated')
});
});
}
這很可能是因為電子正在更新角度變化檢測區域之外的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.