[英]Angular 2 view not updating after receiving ipcRenderer.on from Electron
I'm using ipcRenderer to retrieve the folder path from a browser dialog in my main.js.我正在使用 ipcRenderer 从我的 main.js 中的浏览器对话框中检索文件夹路径。
But for some reason it does not update my the text string on my view.但由于某种原因,它不会更新我视图中的文本字符串。
I know in order for this to work I need to do a setTimeout for it to update (thanks google!).我知道为了让它工作,我需要做一个 setTimeout 来更新(感谢谷歌!)。 But for some reason this does not work.
但由于某种原因,这不起作用。
Code I use is the following.我使用的代码如下。
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);
}
}
Can someone point me in the right direction this is my first app using electron.有人能指出我正确的方向吗这是我第一个使用电子的应用程序。
Kind Regards,亲切的问候,
Thyvo泰沃
For this case, use NgZone
对于这种情况,请使用
NgZone
import { Component,NgZone } from '@angular/core';
Now, update your app within the .run()
function provided by NgZone
现在,在
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')
});
});
} }
This is most likely the because electron is updating the value outside the Angular change detection zone.这很可能是因为电子正在更新角度变化检测区域之外的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.