Selecting a "null" value using mat-option with Angular 7 Reactive Forms Not Working
You can not set the null because you have integer property (user.userId), The sample code should be work.
Template Code:
<form [formGroup]="patientCategory">
<mat-form-field class="full-width">
<mat-select placeholder="Category" formControlName="patientCategory">
<mat-option [value]="0">None</mat-option>
<mat-option *ngFor="let category of patientCategories" [value]="category.id">
{{category.name}} - {{category.description}}
</mat-option>
</mat-select>
</mat-form-field>
<p>{{patientCategory.get('patientCategory').value | json}}</p>
</form>
Componet Code
import { Component, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
/**
* @title Basic table
*/
@Component({
selector: 'table-basic-example',
styleUrls: ['table-basic-example.css'],
templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
patientCategory: FormGroup;
patientCategories = [{
id: 1,
name: 'name 1',
description: 'description 1'
}, {
id: 2,
name: 'name 2',
description: 'description 2'
}, {
id: 3,
name: 'name 3',
description: 'description 3'
}]
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.patientCategory = this.fb.group({
patientCategory: [null, Validators.required]
});
//const toSelect = this.patientCategories.find(c => c.id == 3);
this.patientCategory.get('patientCategory').setValue(0);
}
}
Demo