import {Component, ViewChild, OnInit} from '@angular/core';
import { Observable, Subscription } from 'rxjs';
/**
* @title Basic select
*/
@Component({
selector: 'select-overview-example',
templateUrl: 'select-overview-example.html',
styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample implements OnInit {
@ViewChild('mySelect') mySelect;
foods = [
{value: 'steak-0', viewValue: 'Steak', class: 'highlight'},
{value: 'pizza-1', viewValue: 'Pizza', class: ''},
{value: 'tacos-2', viewValue: 'Tacos', class: 'highlight'}
];
alive = true;
selectedFood: any;
ngOnInit() {
this.selectedFood = this.foods[1];
Observable.timer(0, (10000))
.takeWhile(() => this.alive)
.subscribe(() => {
this.updateData();
});
// PS: if you would like to stop resetting select box, you can assign **this.alive=false;** on a specific logic.
}
updateData() {
console.log('updating data');
this.foods = [
{value: 'steak-0', viewValue: 'Steak', class: ''},
{value: 'pizza-1', viewValue: 'Pizza', class: 'highlight'},
{value: 'tacos-2', viewValue: 'Tacos', class: ''}
];
this.selectedFood = this.foods[0];
console.log(this.selectedFood);
}
click() {
this.mySelect.open();
}
}
/** Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license */
/** No CSS for this example */
.highlight {
background-color: red;
}
<mat-form-field>
<mat-select [(ngModel)] = "selectedFood" #mySelect placeholder="Favorite food">
<mat-option *ngFor="let food of foods" [ngClass]="food.class" [value]="food">
{{ food.viewValue }}
</mat-option>
</mat-select>
</mat-form-field>
<br>
<button (click)="click()">click</button>
<!-- Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license -->