gpt4 book ai didi

angular - 为什么我的值在init(Angular + Electron)上不更新?

转载 作者:行者123 更新时间:2023-12-03 12:29:58 25 4
gpt4 key购买 nike

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>

页面加载时,我的“primaryServer”和“secondaryServer”值不显示。但是,当我单击输入或它的随机按钮时,它会显示出来。我如何让它们出现在init上?

最佳答案

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;
})
})
}
}

这是我为使其正确更新所做的更改,具体说明如下

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

关于angular - 为什么我的值在init(Angular + Electron)上不更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49135736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com