I am new to Angular and I want to call a specific method, only when option3 is selected. I am struggling to solve this and cannot find much information on this on the internet.
When I call option3, the output is empty.
Here is my code so far:
app.component.html:
<mat-form-field>
<mat-label>Select an option</mat-label>
<mat-select [(value)]="selected" >
<mat-option>None</mat-option>
<mat-option value="option1">Option 1</mat-option>
<mat-option value="option2">Option 2</mat-option>
<mat-option selectionChange="greet($event)">Option 3</mat-option>
</mat-select>
</mat-form-field>
<p>You selected: {{selected}}</p>
app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'materialdesign';
selected = 'option33';
greet() {
this.selected = 'it works';
}
}
If you want to listen to an event from an Angular component, you need to add parenthesis around the event:
Then your should method should start executing.