Javascript 角度-电子视图在模型更改后不更新
我有一个字段,允许我从电子对话框中选择文件夹。单击该字段,对话框打开,我可以选择文件夹。 但是,单击“确定”后,即使我的模型包含文件夹的路径,它也不会反映在输入字段中,直到我在字段外单击(当它失去焦点时)。我该如何纠正这种行为 模板包含:Javascript 角度-电子视图在模型更改后不更新,javascript,angular,typescript,electron,angular6,Javascript,Angular,Typescript,Electron,Angular6,我有一个字段,允许我从电子对话框中选择文件夹。单击该字段,对话框打开,我可以选择文件夹。 但是,单击“确定”后,即使我的模型包含文件夹的路径,它也不会反映在输入字段中,直到我在字段外单击(当它失去焦点时)。我该如何纠正这种行为 模板包含: <input type="text" class="form-control" (click)="onClickPath($event)" [(ngModel)]="currentConfiguration.workspacePath" id="work
<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">
配置.component.ts
export class CurrentConfiguration {
workspacePath: string;
}
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];
}
});
}
注意-这几乎是一个骗局,因为它帮助我解决了问题,我将发布答案。 电子对话框似乎在角度区域之外工作,因此对数据的任何更新都不会触发角度刷新视图 为了实现刷新,我必须在如下区域内执行逻辑:
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
}
});
}
您可以尝试注入changeDetectorRef并调用detectesChanges:以强制angular启动新摘要cycle@xrobert35除非某些东西不在角度范围内,否则不需要这样做是的,没错,但由于所有查找最新的东西都在模型中,并且视图仅在事件发生后才发生更改,因此看起来像是一个更改检测问题。我不知道这个电子服务是什么代码,也许它运行在外部scope@xrobert35-我做了更多的研究,看来你确实是对的。帮助我解决了我的问题。