I am using Angular2 - Reactive Forms. Everything works fine until i want to show a pre-populated value in one of fields in Form.
Scenario: There are multiple buttons on a page and each button opens up a form with fields as
- Name
- Message
- Product Code --> Value for this shall be prepopulated as per item code from service.
Failing Scenario: Product Code input value turns to null.
TS Code:
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
queryForm: FormGroup;
constructor(private _productService: ProductService, fb: FormBuilder) {
this.queryForm = fb.group({
'name': [null, Validators.compose([Validators.required, Validators.minLength(5)])],
'email': [
null, [Validators.required, Validators.email]
],
'message': [null,Validators.compose([Validators.required, Validators.minLength(5)])],
'pcode': [
null
],
})
}
HTML Form:
<div *ngFor="let item of product">
<form action="#" [formGroup]="queryForm"
(ngSubmit)="submitForm(queryForm.value)" method="post"
novalidate="" class="text-left note" id="f_{{item.productId}}">
[ .... rest of the fields ...]
<div class="form-group hidden">
<input type="hidden " class="form-control " id="pcode " name="pcode"
formControlName="pcode" [value]="item.productCode" />
</div>
<div class="form-group">
<button type="submit" class="btn1" [disabled]="!queryForm.valid">Submit</button>
</div>
</form>
</div>
How can i achieve this?