I am trying a select a default value in the dropdown. Here the value is an object instead of a string. With a string, the below approach works fine. I am facing issues when I try with an object.
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'awesome-component',
template: `
<select [(ngModel)]="selectedModule">
<option *ngFor="let module of modules" [ngValue]="module">{{module.moduleName}}</option>
</select>
`
})
export class AwesomeComponent implements OnInit {
modules: any[];
selectedModule: any = null;
ngOnInit(){
this.loadModules();
}
loadModules(){
//load you modules set selectedModule to the on with the
//id of modInst.modID[0]._id you can either loop or use .filter to find it.
this.modules = [];
this.modules.push({moduleName:'Ford',_id:1});
this.modules.push({moduleName:'Chevy',_id:2});
this.modules.push({moduleName:'Honda',_id:3});
this.modules.push({moduleName:'Toyota',_id:4});
this.selectedModule = {moduleName:'Toyota',_id:4};
}
}