gpt4 book ai didi

node.js - “setValue”方法在 Angular 应用程序中不起作用

转载 作者:行者123 更新时间:2023-12-04 17:44:48 27 4
gpt4 key购买 nike

我正在使用 MEAN Stack 和 Angular 6 开发一个 Web 应用程序。我的 html 中有一个按钮可以在我单击该按钮时获取默认值,它应该使用默认值填充表单字段。这是我的 html 表单。

    <div style="float: right" id="extrudedHeightPanel" *ngIf="isExtrudedHeight" name="extrudedHeight">
<form #extrudedHeightForm="ngForm" (ngSubmit)="saveExtrudedHeightValue(extrudedHeightForm)" #form="ngForm">
<nb-card class="action-buttons">
<div class="form-input-group">
<div class="row">
<div class="">
<button type='button' (click)="setDefaultValues()" class="btn btn-sm btn-rectangle btn-default text-case">Restore Default</button>
</div>
<div class="">
<button type="submit" class="btn btn-sm btn-rectangle btn-default text-case">Done</button>
</div>
</div>
</div>
</nb-card>
<br>
<br>
<br>
<p>Extruded Height</p>
<br>
<div class="form group">
Extruded Height:
<input type="text" nbInput name="extrudedHeight" [(ngModel)]="extrudedHeight" />
</div>
</form>
</div>

我将数据从 mongo db 获取到我的 .ts 文件,并尝试使用 Angular 中的“setValue”方法将值设置为表单字段。
.ts 文件
    class{

extrudedHeightForm: FormGroup;
ngOnInit()
{
this.extrudedHeightForm = new FormGroup({
extrudedHeight: new FormControl()
});
}

//Set default values for extrudedHeight
setDefaultValues() {
this.extrudedHeightService.getExtrudedHeight("default").subscribe(data => {
this.extrudedHeightForm.setValue({
extrudedHeight:data['extrudedHeight']
});
});
}

}

我的问题是以下部分不起作用。是我做错了还是有什么方法可以达到我的要求。
this.extrudedHeightForm.setValue({
extrudedHeight:data['extrudedHeight']
});

- 更新 -
当我变成 this.extrudedHeightForm.get('extrudedHeight').setValue(data['extrudedHeight']);正如答案中所建议的那样,它也不起作用。为了检查该值,我打印了一个 console.log。
'console.log(this.extrudedHeightForm.get('extrudedHeight'));'部分给出以下值。
Value

但是值 250 不会显示在表单字段中。

最佳答案

尝试

this.extrudedHeightForm.get('extrudedHeight').setValue(data['extrudedHeight']);

您应该在 FormControl 上设置值,而不是在 FormGroup 上。

关于node.js - “setValue”方法在 Angular 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52637935/

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