[英]How to bind id value to form.value and submit data in reactive?
I am using a reactive form.我正在使用反应形式。 I have displayed the company list as a dropdown.
我已将公司列表显示为下拉列表。 I want to take its id only and store it in DB with other form values.
我只想获取它的 id 并将其与其他表单值一起存储在 DB 中。
Angular reactive Angular 无功
<div class="form-group col-sm-6" for="exampleInputPassword1">
<div class="d-block my-3">
<div class="mb-3">
<select class="custom-select" formControlName = "companyName">
<option value="">Choose Company</option>
<option *ngFor="let obj of companyList; let i = index" [value]="companyList[i].companyName">{{obj.companyName}}</option>
</select>
</div>
</div>
</div>
insertUser(){
this.uObj=this.form.value;
this.service.inssertUser("siteBookCreate",this.uObj).subscribe(data=>this.u Obj=data);
}
```
<button type="button" class="btn btn-primary btn-publish"
[disabled]="form.invalid" (click)="inserUser(form.value)">Save <i style="font-size:22px;margin-left:10px;"></i> </button>
not able to get id of company
I guess you have id
property in your company object.我猜您在公司 object 中有
id
财产。 And instead of binding the company name with the select, bind the company object.而不是将公司名称与 select 绑定,而是绑定公司 object。
<div class="form-group col-sm-6" for="exampleInputPassword1">
<div class="d-block my-3">
<div class="mb-3">
<select class="custom-select" formControlName="companyName">
<option value="">Choose Company</option>
<option *ngFor="let obj of companyList; let i = index" [value]="obj">{{obj.companyName}}</option>
</select>
</div>
</div>
</div>
And get it in the TS:并在 TS 中得到它:
insertUser(){
this.uObj = this.form.get('companyName').value.id;
this.service.inssertUser("siteBookCreate",this.uObj).subscribe(data=>this.uObj=data);
}
If you properly configured your form, it should be enough if you add this.uObj=this.form.value.companyName
.如果您正确配置了表单,则添加
this.uObj=this.form.value.companyName
就足够了。 If something else is missing it would be great if you could add stackblitz or something like this.如果缺少其他东西,如果您可以添加 stackblitz 或类似的东西,那就太好了。
change the [value] attribute of your option tag更改选项标签的 [value] 属性
<div class="form-group col-sm-6" for="exampleInputPassword1">
<div class="d-block my-3">
<div class="mb-3">
<select class="custom-select" formControlName = "companyName">
<option value="">Choose Company</option>
<option *ngFor="let obj of companyList; let i = index"
[value]="companyList[i].companyID">
{{obj.companyName}}</option>
</select>
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.