【发布时间】:2020-09-06 23:51:13
【问题描述】:
我正在开发 Angular 应用程序,我已成功从数据库中检索数据并尝试使用 patch Value 方法设置表单控件的值,但不幸的是我遇到了错误 即
core.js:6185 ERROR DOMException: 未能在“HTMLInputElement”上设置“value”属性:此输入元素接受文件名,该文件名只能以编程方式设置为空字符串。
下面是我的代码片段
组件.ts
ngOnInit(): void {
this.EditForm();
this.GetValues() //method where I am retreiving data from firebase and iside it I am calling set Value
}
EditForm() {
this.exampleForm = this.fb.group({
imgurl: ['',Validators.required],
title: ['', Validators.required],
desc: ['', Validators.required],
category: [this.selected, Validators.required],
subcategory: [' ', Validators.required],
name: ['', Validators.required],
privacy: ["true"],
});
}
setFormValue(d4) {
this.imageSrc=d4.imgurl // this I have done to use string interpolation in template
this.exampleForm.patchValue({
imgurl:this.imageSrc,
title:d4.title,
desc:d4.desc,
category:d4.category,
name:d4.name,
privacy:d4.privacy
})
}
component.html
<div class="col-md-8 col-sm-9 col-xs-9">
<form class="create-form" [formGroup]="exampleForm" novalidate (ngSubmit)="onSubmit(exampleForm.value)">
<div class="col-md-4 col-sm-3 col-xs-12">
<input type="file" multiple (change)="detectFiles($event) "
accept="image/x-png,image/gif,image/jpeg"
class="form-control"
formControlName="imgurl">
<img id="imgid"
height="200" width="200"
class="img-rounded"
[src]="imageSrc || 'http://placehold.it/180'" alt="your image" />
</div>
//rest of form controls
但是当我看到我的提交按钮被禁用时,即使所有表单控件都具有价值。当我在检查器中检查时,我发现 img 控制器有 ng-invalid。我不确定的原因可能是什么。请在这里帮助我
【问题讨论】:
-
这个答案可能会有所帮助:stackoverflow.com/questions/49970970/…
标签: angular angular2-template angular-reactive-forms angular2-forms angular-forms