For some reason category is returning undefined.
component
<form [formGroup]="searchForm" class="login-form d-flex flex-column m-auto">
<mat-form-field appearance="outline">
<mat-label>Category</mat-label>
<mat-select placeholder="Product type" formControlName="category" (valueChange)="search()" >
<mat-option *ngFor="let cate of this.categories" [value]="cate">{{ cate }}</mat-option>
</mat-select>
</mat-form-field>
</form>
And the components TypeScript
import { Component } from '@angular/core';
import { Products } from "../../Products";
import { ProductService } from "../../services/product.service";
import {ShoppingCartService} from "../../services/shopping-cart.service";
import {BreakpointObserver, Breakpoints} from "@angular/cdk/layout";
import {FormBuilder, FormControl, FormGroup} from "@angular/forms";
@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: ['./products.component.css']
})
export class ProductsComponent {
searchForm: FormGroup;
category: string;
categories: string[] = ['CPU', 'GPU', 'HDD'];
searchForm: FormGroup;
constructor() {}
search(): void {
console.log(this.category); // undefined
}
}
I tried binding it many other ways but no more what I do it always returning undefined
[(value)]and not[value]. And since you are using angular forms, you would need to use[(ngModel)]on the select element