【发布时间】:2021-04-09 15:14:51
【问题描述】:
我有一个应用程序(生成 jhipster 的前端角度后端 Java Spring boot)。还有一种反应形式,我可以节省材料,例如螺丝之类的东西。但我似乎无法将它与我的材料模型类正确连接我只能保存一个属性,那就是数量。我试图用 [(ngModel)] 解决它,这是我尝试过的,但它只适用于数量。卖家名称和价格金额也可以正常工作。我不明白为什么一个属性有效而其他属性无效,我似乎找不到错误。
<div class="input-group input-group-sm col-12 col-md-6 mb-4">
<div class="input-group-prepend">
<span class="input-group-text" id="materialName">Name</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-sm"
formControlName="name"
[(ngModel)]="materialModel.name" name="name">
</div>
<div class="input-group input-group-sm col-12 col-md-6 mb-4">
<div class="input-group-prepend">
<span class="input-group-text" id="materialCode">Material No</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-sm"
formControlName="articleNumber"
name="articleNumber"
[(ngModel)]="materialModel.articleNumber">
</div>
</div>
<div class="form-row mb-4">
<div class="input-group input-group-sm col-12 col-md-6 mb-4">
<div class="input-group-prepend">
<span class="input-group-text" id="quantity">Quantity</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
formControlName="quantity"
[(ngModel)]="materialModel.quantity" name="quantity"
aria-describedby="inputGroup-sizing-sm">
</div>
<div class="divider"></div>
<div class="-pull-right">
<button type="button" class="btn btn-primary mb-2" (click)="addSellerInput()">Add Seller</button>
</div>
<div class="form-row mb-4" formArrayName="buyInformation"
*ngFor="let buyInformation of buyInformation().controls; let i = index">
<div class="input-group input-group-sm col-12 col-md-6 mb-4" [formGroupName]="i">
<div class="input-group-prepend">
<span class="input-group-text" id="sellerName">Seller Name</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-sm"
formControlName="seller"
[(ngModel)]="sellerModel.name">
</div>
<div class="input-group input-group-sm col-12 col-md-6 mb-4" [formGroupName]="i">
<div class="input-group-prepend">
<span class="input-group-text" id="materialUnitPrice">Price</span>
</div>
<input type="text" class="form-control" aria-label="Sizing example input"
aria-describedby="inputGroup-sizing-sm"
formControlName="price"
name="price"
[(ngModel)]="priceModel.amount">
</div>
</div>
这是我的 ts 文件:
materialAddForm: FormGroup;
formData: any;
@Input()
materialModel: MaterialModel
@Input()
priceModel: PriceModel
@Input()
sellerModel: SellerModel
constructor(
private materialService: MaterialService,
private priceService: PriceService,
private sellerService: SellerService,
private fb: FormBuilder
) {
this.materialModel = new MaterialModel()
this.priceModel = new PriceModel()
this.sellerModel = new SellerModel()
}
ngOnInit() {
this.materialAddForm = this.fb.group({
name: new FormControl(''),
articleNumber: new FormControl(''),
materialType: new FormControl(''),
quantity: new FormControl(''),
buyInformation: this.fb.array([])
})
}
buyInformation(): FormArray {
return this.materialAddForm.get("buyInformation") as FormArray
}
saveMaterial() {
this.sellerService.addSeller(this.sellerModel).subscribe(console.log);
this.priceService.addPrice(this.priceModel).subscribe(console.log);
this.materialService.addMaterial(this.materialModel).subscribe(console.log);
this.materialAddForm.reset()
this.buyInformation().clear()
console.log(this.materialModel)
}
newBuyForm() {
return this.fb.group(({
seller: new FormControl(''),
price: new FormControl(''),
}));
}
addSellerInput() {
this.buyInformation().push(this.newBuyForm())
}
还有我的 Material 模型:
export class MaterialModel{
constructor(
public id?: number,
public name?: string,
public articleNumber?: number,
public quantity?: number,
public price?: number,
public imageURL?: string,
public materialType?: string
) {}
}
到目前为止,材料中只有数量起作用。
【问题讨论】:
标签: angular typescript spring-boot jhipster