I have a select box fed by a database and I want to pass the selected value to the an input. Example: I select a category and the selected value shows up in the "type" field. Is there any way to achieve this?
<form
fxLayout="row"
fxLayoutAlign="center center"
fxLayoutGap="10px"
#form="ngForm"
autocomplete="off"
(submit)="onSubmit(form)">
<input type="hidden" name="id" [value]="service.formData.id">
<mat-form-field appearance="fill">
<mat-label>Name</mat-label>
<input
matInput
name="name"
#name="ngModel"
[(ngModel)]="service.formData.name"
class="form-control"
placeholder="name"
required>
</mat-form-field>
<mat-form-field appearance="fill" i>
<mat-label>Category</mat-label>
<input
matInput
name="categoryId"
#categoryId="ngModel"
ngModel
[(ngModel)]="service.formData.categoryId"
class="form-control"
placeholder=""
required
>
</mat-form-field>
<mat-form-field appearance="fill">
<mat-label>Type</mat-label>
<input
matInput
name="type"
#type="ngModel"
[(ngModel)]="service.formData.type"
class="form-control"
placeholder="Type"
required>
</mat-form-field>
<div class="form-group">
<button class="btn-success btn-lg btn-block" type="submit">Submit</button>
</div>
</form>
<form
*ngIf="!isAddingCategory"
fxLayout="row"
fxLayoutAlign="center center"
fxLayoutGap="10px"
#form="ngForm"
autocomplete="off" >
<mat-label >Can't find your category?</mat-label>
<div class="form-group">
<button class="btn-success btn-block" (click)="changeIsAddingCategory()">Add New Category</button>
</div>
</form>
<div>
<mat-form-field *ngIf="!isAddingCategory" appearance="fill">
<mat-label>Category</mat-label>
<mat-select [(ngModel)]="selectedOption" (selectionChange) = updateCategory(event)>
<mat-option *ngFor="let category of categories | async " [value]="category.id" >
{{ category.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
ngOnInit(): void {
this.isAddingCategory = false;
this.resetForm();
this.records = this.service.getRecordsForForm();
this.categories = this.service.getCategories();
console.log(this.records)
this.service.categoryFormData = {
id: 0,
name: ""
}
}
onSubmit(form: NgForm) {
console.log(form.value)
if (this.service.formData.id == 0)
this.insertRecord(form)
else
this.updateRecord(form)
}
insertRecord(form: NgForm) {
console.log("I'm the inserted object = " + form.value)
this.service.postRecordDetail().subscribe(
res => {
this.resetForm(form);
this.service.getRecords();
},
err => {
console.log(err);
}
);
}

selectionChangeevent to a function to update the value. material.angular.io/components/select/api