enter code here
控制台没有显示任何错误,功能onsubmit()可以正常工作并且创建了项目,但是我可以在下拉列表中看到shop.id旁边的shop.name。而且我只能看到ID。如何更改我的代码以显示名称?
<form [formGroup]="priceForm" (ngSubmit)="onSubmit()">
<label>
Shop:
<select formControlName="shop_id" required>
<option *ngFor="let shop of shops" [ngValue]="shop.id">
{{shop.id}} {{shop.name}}
</option>
</select>
</label>
<button type="submit" [disabled]="!priceForm.valid" >Submit</button>
</form>
// price-editor.component.ts
导出类PriceEditorComponent实现OnInit {
getShops():无效{
this.shopService.getShops()
.subscribe(shops => this.shops = shops);
}
priceForm = this.fb.group({
price: ['', Validators.required],
shop_id: ['',Validators.required]
});
商店:商店[];
myprice:价格; 得到f(){
return this.priceForm.controls ;
} onSubmit(){
如果(this.priceForm.invalid)
{
return;
} this.priceForm.get('price')。value};
this.myprice = {price: this.f.price.value,
shopID: this.f.shop_id.value
};
this.priceService.create(this.myprice)
.subscribe(
error => {console.log(this.priceForm.value);}
);
}
ngOnInit(){
this.getShops();
} }
// shop.service.ts
getShop(id:number):
可观察的{
const url = ${this.shopsUrl}/${id}
;
返回this.http.get(url).pipe(
tap(_ => this.log(`fetched shop id=${id}`)),
catchError(this.handleError<Shop>(`getShop id=${id}`))
); }