Goal: Set default value for a dropdown that changes the address of the site the user is on /en/ or /es/ for English or Spanish
Problem: After going through documentation and through every SO article I could find such as Angular 2 Dropdown Options Default Value, none of the variations seem to work.
What is wrong? And do I need a form/mat-form or is there a simpler way to do this?
What I've tried: Here are some of the variations of what I've tried in the HTML and in the TypeScript:
<mat-select (selectionChange)="doSomething($event)" [value]="language">
<mat-select (selectionChange)="doSomething($event)" [value]="English">
<mat-select (selectionChange)="doSomething($event)" [(ngModel)]='defaultLanguage'>
HTML:
<form [formGroup]="_siteForm">
<mat-form-field class="right">
<mat-select (selectionChange)="doSomething($event)" [(ngModel)]='defaultLanguage'>
<mat-option *ngFor="let language of languages" [value]="language">
{{language.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</form>
TypeScript:
public _siteForm : FormGroup;
this._siteForm = fb.group({
'languages' : ['']
});
public languages = [
{ value: "en", viewValue: 'English' },
{ value: "es", viewValue: 'Spanish' }
];
public defaultLanguage : string = "English";
if (this.localeId == "en") { this._siteForm.controls["languages"].setValue(this.languages.filter(item => item.value === 'en')[0].viewValue); }
else { this._siteForm.controls["languages"].setValue(this.languages.filter(item => item.value === 'es')[0].viewValue); }
Alternate
this._siteForm.setValue({ languages: "en" });