Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access properties of angular material components?

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?

like image 575
sehrcooler Avatar asked Apr 12 '18 09:04

sehrcooler


People also ask

Which features provided by angular material?

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.

What are material components in angular?

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.

How do I get mat select value?

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.


2 Answers

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

like image 137
Yerkon Avatar answered Oct 21 '22 16:10

Yerkon


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

like image 4
Tomasz Kula Avatar answered Oct 21 '22 17:10

Tomasz Kula