I am new to angular. I am having trouble understanding the functionality of mat-select and mat-option elements.
I want my mat-select to fetch the data ONLY when clicked. But right now I have to click twice in order to make the list appear and I really don't understand why. If anyone has any idea I would be grateful.
My HTML:
<mat-select placeholder="Aircraft Family" [(ngModel)]="aircraftFamily" (click)="loadFamilies()">
<mat-option *ngFor="let myAircraftFamily of aircraftFamilies" [value]="myAircraftFamily.name">{{myAircraftFamily.name}}</mat-option>
</mat-select>
My Module:
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
aircraftFamily: string = '';
aircraftFamilies = [];
constructor(private route: ActivatedRoute, private headerService: HeaderService) {
this.route.paramMap.subscribe(
(params: ParamMap) => {
if (params.has('family')) {
this.aircraftFamily = params.get('family');
}
}
);
}
ngOnInit() {
}
loadFamilies() {
this.headerService.getAircraftFamilies().then((result) => {
this.aircraftFamilies = result; // [{id:xxx, name:aircraftX},..]
}, (error: HttpErrorResponse) => {
console.log('error: ' + error)
});
}
}