Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a callback on material-angular-select?

Surprisingly I couldn't find a onselect callback, So how do I create a callback on material-angular-select ?

This is my attempt

import { Component, Input, Output, OnInit, OnChanges, SimpleChanges, ChangeDetectionStrategy, EventEmitter } from '@angular/core';    
import { BehaviorSubject } from 'rxjs/BehaviorSubject';    
import { FormControl } from '@angular/forms';
import { MatSelectModule } from '@angular/material/select';

@Component({
  selector: 'app-search-navigator',
  template: `
    <mat-form-field>
      <mat-select [(value)]="selected" [formControl]="pc" panelClass="example-panel-{{pc.value}}">
        <mat-option *ngFor="let panelColor of panelColors" [value]="panelColor.value">
          {{ panelColor.viewValue }}
        </mat-option>
      </mat-select>
    </mat-form-field>
  `,
  changeDetection: ChangeDetectionStrategy.Default
})
export class SearchNavigatorComponent implements OnInit {
  private selected ;
  private selectedObs$;
  private pc = new FormControl('red');
  private panelColors = [
    {value: 'red', viewValue: 'red'},
    {value: 'blue', viewValue: 'blue'},
    {value: 'green', viewValue: 'green'}
  ];
  constructor() {}

  ngOnInit() {
    this.selectedObs$ = new BehaviorSubject<any>(this.selected);
    this.selectedObs$.subscribe((aselected) => {
      console.log(aselected);//Nothing happens on select :(
    });
  }
}
like image 391
ishandutta2007 Avatar asked Nov 30 '17 08:11

ishandutta2007


People also ask

How do I use selectionChange in mat select?

The event selectionChange is used with <mat-select> element as following. Our function onBookChange() will execute every time when there is selection change. To get the selected value we can pass $event to the function. We can also get selected value using FormControl , FormGroup and NgModel .

What is a mat select trigger?

The MatSelectTrigger directive, who's selector is “mat-select-trigger”, allows us to customize the trigger that is displayed when the select has a value. The trigger can be the Select itself, or another control, such a button, or any interactive page element really.

How do you readonly mat select?

Create a form with fieldset in it and mat-select in it (form -> fieldset -> mat-select) Add [disabled] to fieldset inside form tag to be true on submit. Force disabled to become true.


2 Answers

Material select emits MatSelectChange event on every change. It's outputed as selectionChange.

 <mat-form-field>
      <mat-select [(value)]="selected" [formControl]="pc" panelClass="example-panel-{{pc.value}}" (selectionChange)="doSomething($event)">
        <mat-option *ngFor="let panelColor of panelColors" [value]="panelColor.value">
          {{ panelColor.viewValue }}
        </mat-option>
      </mat-select>
    </mat-form-field>
like image 162
Mateusz Witkowski Avatar answered Oct 04 '22 22:10

Mateusz Witkowski


I had a similar problem and still needed the value to be subscribable in other components via a Service. Also, in my case the select is not part of a form. So I thought I'll drop my solution for angular/material 5, in case anyone else stumbles upon this:

my.service.ts

@Injectable()
export class MyService {
    myValue: BehaviorSubject<number> = new BehaviorSubject(1);
}

This service is defined as a global provider in app.modules.ts

some.component.ts has the <mat-select>

@Component({
    templateUrl: './some.component.html',
})
export class SomeComponent {
    constructor(private __myService: MyService) {
    }

    selectValue = this.__myService.myValue.value;

    changeValue($event: EventEmitter<MatSelectChange>) {
        this.__myService.myValue.next($event.value);
    }
}

some.component.html

<mat-select
    [value]="selectValue"
    (selectionChange)="changeValue($event)"
    placeholder="Select a value"
>
    <mat-option [value]="1">Option 1</mat-option>
    <mat-option [value]="2">Option 2</mat-option>
    <mat-option [value]="3">Option 3</mat-option>
</mat-select>

other.component.html subscribes to value changes

export class OtherComponent implements OnInit {
    constructor(private __myService: MyService) {
    }

    ngOnInit() {
        this.__myService.myValue.subscribe(
            next => {
                // do something
            }
        );
    }
}
like image 38
masterfloda Avatar answered Oct 04 '22 22:10

masterfloda