简体   繁体   English

从 Electron 接收 ipcRenderer.on 后,Angular 2 视图未更新

[英]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.

相关问题 如何注销 ipcRenderer.on 事件监听器? - How to unregister from ipcRenderer.on event listener? Angular &amp; Electron:ipcRenderer - __dirname 未定义 - Angular & Electron: ipcRenderer - __dirname is not defined 如何在电子桌面应用程序中通过 IpcRenderer 从角度特征组件向电子主进程发送消息 - how to send message from angular feature component to electron main process via IpcRenderer in electron desktop application 如何将Electron ipcRenderer集成到基于TypeScript的Angular 2应用程序中? - How to integrate Electron ipcRenderer into Angular 2 app based on TypeScript? 如何使用 observables 在 electron ipcMain 和 angular ipcRenderer 之间异步发送数据 - How to send data between electron ipcMain and angular ipcRenderer asynchronously with observables 超时后 Angular 5 视图未更新 - Angular 5 View Not Updating After Timeout Angular 6在POST后不更新视图 - Angular 6 not updating the view after POST 从服务器调用angular 4更新对象后,视图未更新 - view is not being updated after updating object from server call angular 4 从对象数组中删除元素后,Angular 2视图不会更新 - Angular 2 view not updating after removing elements from object array 从另一个组件设置模型后,Angular2 视图不会更新 - Angular2 view not updating after model is set from another component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM