gpt4 book ai didi

javascript - Angular - 模型更改后 Electron View 不会更新

转载 作者:行者123 更新时间:2023-12-03 01:04:21 27 4
gpt4 key购买 nike

我有一个字段,使我能够从 Electron 对话框中选择文件夹。单击该字段后,将打开对话框,我可以选择文件夹。但是,点击“确定”后,即使我的模型包含文件夹的路径,它也不会反射(reflect)在输入字段中,直到我单击字段外部(当它失去焦点时)。我到底该如何解决这个问题?

模板包含:

<input type="text" class="form-control" (click)="onClickPath($event)" [(ngModel)]="currentConfiguration.workspacePath" id="workspace-path" name="workspace-name" aria-label="workspace" aria-describedby="workspace-lable">

CurrentConfiguration.ts

export class CurrentConfiguration {
workspacePath: string;
}

configuration.component.ts

currentConfiguration: CurrentConfiguration = {
workspacePath: ""
};

//onClickedPath event:

onClickPath(event) {
console.log("clicked: ", event.target.id);

// open the dialog to select the directory
const dir = this.electronService.remote.dialog.showOpenDialog({
properties: ["openDirectory"],
title: event.target.id.split('-').join(" ")
}, (folderPath) => {
console.log(folderPath);

if (folderPath[0] == undefined) {
this.electronService.remote.dialog.showMessageBox({
type: "error",
buttons: ["ok"],
title: "Error",
message: "Please select the folder"

});
}
else{
// set the correct directoryPath.
this.currentConfiguration[event.target.id.split('-')[0] + 'Path'] = folderPath[0];
}
});

}

最佳答案

注意 - 这几乎是 This question 的骗局由于它帮助我解决了问题,我将发布答案。

Electron 对话框似乎在 Angular 区域之外运行,因此对数据的任何更新都不会触发 Angular 刷新 View 。

为了使刷新发生,我必须在区域内执行逻辑,如下所示:

import { NgZone } from '@angular/core';
...
...
currentConfiguration: CurrentConfiguration = {
workspacePath: ""
};

//onClickedPath event:

onClickPath(event) {
console.log("clicked: ", event.target.id);

// open the dialog to select the directory
const dir = this.electronService.remote.dialog.showOpenDialog({
properties: ["openDirectory"],
title: event.target.id.split('-').join(" ")
}, (folderPath) => {
console.log(folderPath);

if (folderPath[0] == undefined) {
this.electronService.remote.dialog.showMessageBox({
type: "error",
buttons: ["ok"],
title: "Error",
message: "Please select the folder"

});
}
else{
// run all of this inside the zone
this.zone.run(() => {
// set the correct directoryPath.
this.currentConfiguration[event.target.id.split('-')[0] + 'Path'] = folderPath[0];
}); // end of zone
}

});

}

关于javascript - Angular - 模型更改后 Electron View 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52481383/

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