【发布时间】:2021-12-08 01:28:57
【问题描述】:
我想在编辑时将文件路径显示为输入值
原因是当我点击编辑按钮时,营地的所有其他值(数据)都被放入输入字段,但营地的 url 没有放入输入字段
这是我的 camp.ts 文件
form = new FormGroup({
title:new FormControl('',),
images: new FormControl(''),
location: new FormControl(''),
price: new FormControl(''),
description: new FormControl('')
})
public uploadFile(event: Event): void {
const input = event.target as HTMLInputElement;
if (!input.files?.length) {
return;
}
const file = input.files[0];
this.form.patchValue({
images: file
});
this.form.get('images');
}
ngOnInit(): void {
this.id = this.route.snapshot.params.id;
this.getData();
}
get f(){
return this.form.controls;
}
getData(){
this.campgroundService.getDataById(this.id).subscribe(res => {
this.data = res;
this.campground = this.data;
this.form = new FormGroup({
title: new FormControl(this.campground.title, Validators.required),
images: new FormControl(this.campground.images[0].url , Validators.required),
location: new FormControl(this.campground.location, Validators.required),
price: new FormControl(this.campground.price, Validators.required),
description: new FormControl(this.campground.description, Validators.required)
});
console.log('Fetching Form Values from backend',this.form.value);
console.log('file data = ', this.campground.images[0].url);
});
}
这是我的 camp.html 文件
<div class="form-group" >
<label for="title"> Enter Title </label>
<input type="text" formControlName="title" placeholder="Enter Title" [ngClass]="{'is-invalid':submitted && f.title.errors}" class="form-control">
<div *ngIf="submitted && f.title.errors" class="invalid-feedback">
<div *ngIf="f.title.errors.required">
Title Is Required!
</div>
</div>
</div>
<!-- Image Input -->
<div class="form-group">
<label for="image"> Selcet Image </label>
<input type="file" name="images" id="image" (change)="uploadFile($event)" class="form-control" accept="image/x-png,image/gif,image/jpeg"/>
</div>
【问题讨论】: