I am attempting to populate a select form field from an HTTP call. I get the data but cannot seem to set the value correctly utilizing a FormArray.
I have tried adjusting my object calls.references and even utilized array notation. I can get it to dump to the console, but not populate the select form field. So I am thinking that something is incorrect in my Angular code, but I have been unsuccessful in finding an example of this online. All of the documentation/tutorials I have come across do not have selects in the form.
public ngOnInit () {
// Reactive form fields
this.timecardForm = this.fb.group( {
payBeginningDate: [ '2019-03-19', [ Validators.required ] ],
payEndingDate: [ '2019-03-26', [ Validators.required ] ],
payCategoriesTracked: this.fb.array( [ this.buildPayCategoriesTracked() ] ), // This is the one on which I am working
overtimeCategories: '1',
workHoursTracked: this.fb.array( [ this.buildWorkTracked() ] ),
earnings: [ { value: null, disabled: true }, [ Validators.required ] ],
totalHours: [ { value: null, disabled: true }, [ Validators.required ] ],
totalEarnings: [ { value: null, disabled: true }, [ Validators.required ] ]
} );
... // There is some more unrelated code
// Dynamically build payCategories row
buildPayCategoriesTracked (): FormGroup {
console.log( 'buildPayCategoriesTracked: ', this.timeEntry.payCategories ); // This prints to the console successfully
return this.fb.group( {
payCategories: [ this.timeEntry.payCategories, [ Validators.required ] ]
} );
}
<!-- The HTML in question... -->
<select formControlName="{{i}}"
id="{{ 'payCategoriesTracked' + i }}"
class="form-control"
(change)="onRecordUpdated(timeCard, timecardDet)"
[ngClass]="{'is-invalid': payCategoriesMessage }">
<option value=null
disabled
selected
hidden>--Select--</option>
<option *ngFor="let payCategory of payCategories"
[ngValue]="payCategoryDescription.payCategoryId">{{payCategoryDescription}}</option>
</select>
I simply want my payCategories to populate my select form field.An example of one of the items returned is:
{payCategoryId: 9, description: "DUE FROM LIBRAR", payType: "Hourly", isActive: true}
So I want the select value to be the id and the description to display in the options tag.
Update I have changed my HTML as follows...
<div *ngFor="let payCategoriesTracked of payCategoriesTracked.controls;let i=index">
<mat-form-field>
<mat-label>Pay Category</mat-label>
<mat-select formControlName="i"
(change)="onRecordUpdated(timeCard, timecardDet)"
[ngClass]="{'is-invalid': payCategoriesMessage }">
<mat-option *ngFor="let payCategory of payCategories"
[ngValue]="payCategory.value">
{{payCategory.description}}
</mat-option>
</mat-select>
</mat-form-field>
Solution I figured it out...woot! Here is my solution...
<mat-form-field>
<mat-label>Pay Category</mat-label>
<select matNativeControl
formControlName="payCategory">
<option *ngFor="let payCategory of payCategories"
[ngValue]="payCategory">
{{payCategory.description}}
</option>
</select>
</mat-form-field>
// This is the FormControl, which is within a formgroup...
payCategory: new FormControl( this.buildPayCategories() )
// Pull in payCategories for select list from service
buildPayCategories () {
this.payCategories = this.timeEntry.payCategories;
return this.payCategories;
}
<select>? As well as the data that is binded to it?