0

setting.component.ts

import { Component, OnInit } from '@angular/core';
import { ElectronService } from 'ngx-electron';

@Component({
  selector: 'app-setting',
  templateUrl: './setting.component.html',
  styleUrls: ['./setting.component.css']
})
export class SettingComponent implements OnInit {
  private primaryServer: string;
  private secondaryServer: string;

  constructor(
    private electron: ElectronService
  ) { }

  ngOnInit() {
    this.electron.ipcRenderer.send("asynchronous-message", {get: "settings"})
    this.electron.ipcRenderer.on("asynchronous-reply", (event, data) => {
      this.primaryServer = data.database.primary;
      this.secondaryServer = data.database.secondary;
    })
  }
}

setting.component.html

<p>{{primaryServer}}</p>
<p>{{secondaryServer}}</p>

My "primaryServer" and "secondaryServer" values do not show up when the page loads. however, when I click on the input or a random button for it, it shows up. How do I get them to show up on init?

Eric Chu
  • 1,649
  • 3
  • 20
  • 26

1 Answers1

5
import { Component, OnInit, NgZone } from '@angular/core';
import { ElectronService } from 'ngx-electron';

@Component({
  selector: 'app-setting',
  templateUrl: './setting.component.html',
  styleUrls: ['./setting.component.css']
})
export class SettingComponent implements OnInit {
  private primaryServer: string;
  private secondaryServer: string;

  constructor(
    private electron: ElectronService,
    private zone: NgZone
  ) { }

  ngOnInit() {
    this.electron.ipcRenderer.send("asynchronous-message", {get: "settings"})
    this.electron.ipcRenderer.on("asynchronous-reply", (event, data) => {
      this.zone.run(()=>{
        this.primaryServer = data.database.primary;
        this.secondaryServer = data.database.secondary;
      })
    })
  }
}

This is the changes I made for it to update properly as said by

Your code might run outside of Angulars zone and therefore Angular doesn't recognize it needs to run change detection. You can use zone.run(() => { your code that updates the model here }) to force code back into the zone. If this works you can investigate further why it runs outside Angulars zone. – Günter Zöchbauer

Eric Chu
  • 1,649
  • 3
  • 20
  • 26
  • 1
    Thank you for including the final result that worked for you based on your question, I think a lot of stack-overflow questions/answers could benefit from this format! – Jessy Oct 20 '19 at 14:04