Thy*_*yvo 5 ipc settimeout electron angular
我正在使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
有人能指出我正确的方向吗这是我第一个使用电子的应用程序。
亲切的问候,
泰沃
对于这种情况,使用NgZone
import { Component,NgZone } from '@angular/core';
Run Code Online (Sandbox Code Playgroud)
现在,在.run()以下提供的功能中更新您的应用程序NgZone
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')
});
});
Run Code Online (Sandbox Code Playgroud)
}
这很可能是因为电子正在更新角度变化检测区域之外的值。
| 归档时间: |
|
| 查看次数: |
841 次 |
| 最近记录: |