我想在带有 ngModel 属性的下拉列表中显示默认值。但我没有得到默认值
I want to display default value in drop-down with ngModel attribute. but I am not getting default value
{{language.localeCode}}l
使用下面的代码,在 html app.component.html
<select (change)="selectlang()" [(ngModel)]="selectedlang">
<option *ngFor="let lang of details">{{lang.localeCode}}</option>
</select>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
selectedlang='EN'
details =[{
name:'a',
language:'english',
localeCode:'EN'
},
{
name:'b',
language:'arabic',
localeCode:'AR'
}
]
selectlang(){
console.log(this.selectedlang)
}
}
你需要给一个值selectedlang='EN'(你喜欢)
使用下面的代码,在 html app.component.html
<select (change)="selectlang()" [(ngModel)]="selectedlang">
<option *ngFor="let lang of details">{{lang.localeCode}}</option>
</select>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
selectedlang='EN'
details =[{
name:'a',
language:'english',
localeCode:'EN'
},
{
name:'b',
language:'arabic',
localeCode:'AR'
}
]
selectlang(){
console.log(this.selectedlang)
}
}
你需要给一个值selectedlang='EN'(你喜欢)