I'm fairly new to angular in general, i have built my first few apps with it and right now I'm working on some project that contains angular material.
When i go to this site i see lots of properties of the MatSelect directive. There is one property called 'empty: boolean' that I would like to access in some way, but i don't know how, can you help me?
Angular Material provides a set of reusable UI components based on the Material Design system. Angular Material is composed of several pieces. It has a CSS library for typography and other elements, it provides an interesting JavaScript approach for theming, and its responsive layout uses a flex grid.
Angular Material comprises a range of components which implement common interaction patterns according to the Material Design specification. Form Controls Controls that collect and validate user input. Navigation Menus, sidenavs and toolbars that organise your content.
To add elements to Select option, we need to use <mat-option> element and to bind value with <mat-option> , use value property of it. To set and get a value for <mat-select> , use value , ngModel , formControl and formControlName property.
Pay attention to Exported as:matSelect
. You can reference it by template reference variable(#var) or by ViewChild:
<mat-select #matSelect = 'matSelect'>
...
component.ts:
@ViewChild('matSelect') child: MatSelect;
//or
@ViewChild(MatSelect) child: MatSelect;
https://material.angular.io/components/select/api#MatSelect
Demo Example
You can use the @ViewChild
decorator. Query for the MatSelect
component imported from @angular/material
. Keep in mind that elements queried by the @ViewChild
decorator are available after the view is init (hence the ngAfterViewInit
lifecycle hook).
select.overview.html
<mat-form-field>
<mat-select placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [value]="food.value">
{{ food.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
select.overview.ts
import {Component, ViewChild, AfterViewInit} from '@angular/core';
import {MatSelect} from '@angular/material';
@Component({
selector: 'select-overview-example',
templateUrl: 'select-overview-example.html',
styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample implements AfterViewInit{
@ViewChild(MatSelect) select: MatSelect;
foods = [
{value: 'steak-0', viewValue: 'Steak'},
{value: 'pizza-1', viewValue: 'Pizza'},
{value: 'tacos-2', viewValue: 'Tacos'}
];
ngAfterViewInit() {
console.log(this.select.empty)
}
}
Live demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With