admin 管理员组文章数量: 1086019
I try to add a default value to an option. It will be like a kind of placeholder and I use this method to do it.
In pure HTML it work, but if I try to use *ngFor
of angular 2 attribute, it doesn't select anything.
Here my code that I use in pure HTML:
<select name="select-html" id="select-html">
<option value="0" selected disabled>Select Language</option>
<option value="1">HTML</option>
<option value="2">PHP</option>
<option value="3">Javascript</option>
</select>
And using Angular template:
<select name="select" id="select" [(ngModel)]="selectLanguage">
<option *ngFor="let item of selectOption"
[selected]="item.value==0"
[disabled]="item.value==0">{{item.label}}</option>
</select>
The class is
import {Component} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-form-select',
templateUrl: 'defaultponent.html'
})
export class DefaultComponent {
private selectOption: any;
constructor() {
this.selectOption = [
{
id: 1,
label: "Select Language",
value: 0
}, {
id: 2,
label: "HTML 5",
value: 1
}, {
id: 3,
label: "PHP",
value: 2
}, {
id: 4,
label: "Javascript",
value: 3
}
]
}
}
I want Select Language
to be selected as a default value. It's disabled but not selected.
How can I select it as a default value?
Live example
I try to add a default value to an option. It will be like a kind of placeholder and I use this method to do it.
In pure HTML it work, but if I try to use *ngFor
of angular 2 attribute, it doesn't select anything.
Here my code that I use in pure HTML:
<select name="select-html" id="select-html">
<option value="0" selected disabled>Select Language</option>
<option value="1">HTML</option>
<option value="2">PHP</option>
<option value="3">Javascript</option>
</select>
And using Angular template:
<select name="select" id="select" [(ngModel)]="selectLanguage">
<option *ngFor="let item of selectOption"
[selected]="item.value==0"
[disabled]="item.value==0">{{item.label}}</option>
</select>
The class is
import {Component} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-form-select',
templateUrl: 'default.component.html'
})
export class DefaultComponent {
private selectOption: any;
constructor() {
this.selectOption = [
{
id: 1,
label: "Select Language",
value: 0
}, {
id: 2,
label: "HTML 5",
value: 1
}, {
id: 3,
label: "PHP",
value: 2
}, {
id: 4,
label: "Javascript",
value: 3
}
]
}
}
I want Select Language
to be selected as a default value. It's disabled but not selected.
How can I select it as a default value?
Live example
Share Improve this question edited May 23, 2017 at 12:26 CommunityBot 11 silver badge asked Sep 27, 2016 at 12:03 Radonirina MaminiainaRadonirina Maminiaina 7,0044 gold badges35 silver badges63 bronze badges4 Answers
Reset to default 27update
4.0.0-beta.6 added compareWith
<select [compareWith]="compareFn" [(ngModel)]="selectedCountries">
<option *ngFor="let country of countries" [ngValue]="country">
{{country.name}}
</option>
</select>
compareFn(c1: Country, c2: Country): boolean {
return c1 && c2 ? c1.id === c2.id : c1 === c2;
}
this way the instance assigned to selectedCountries
doesn't need to be the identical object, but a custom comparison function can be passed, for example to be able to match an different object instance with identical property values.
original
If you want to use an object as value you need to use [ngValue]
on the option element.
<select name="select" id="select" [(ngModel)]="selectLanguage">
<option *ngFor="let item of selectOption" [ngValue]="item"
[disabled]="item.value==0">{{item.label}}</option>
</select>
When selectLanguage
has an options value assigned [(ngModel)]="..."
will this one make the one selected by default:
import {Component} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-form-select',
templateUrl: 'default.component.html'
})
export class DefaultComponent {
private selectOption: any;
constructor() {
this.selectOption = [
{
id: 1,
label: "Select Language",
value: 0
}, {
id: 2,
label: "HTML 5",
value: 1
}, {
id: 3,
label: "PHP",
value: 2
}, {
id: 4,
label: "Javascript",
value: 3
}
];
this.selectLanguage = this.selectOption[0];
}
}
I find it nicer to remove the default option from the selectOption
array and specifying the default unselectable option separately.
<select name="select" id="select" [(ngModel)]="selectLanguage">
<option [ngValue]="undefined" disabled>Select Language</option>
<option
*ngFor="let item of selectOption"
[value]="item.value"
>
item.label
</option>
</select>
Try below sample code for this:
Replace yourValue
with whatever value you want to select default
<select placeholder="country" >
<option *ngFor="let country of countriesList" [ngValue]="country" [selected]="country.country_name == yourValue" >
{{country.country_name}}
</option>
</select>
<select class="form-control" [(ngModel)]="someList.key" name="key" #key="ngModel" required>
<option value="undefined" selected disabled>Select option</option>
<option *ngFor="let data of someList" value="{{data.key}}">{{data.key}</option>
</select>
本文标签: javascriptAngular 2 set default value to select optionStack Overflow
版权声明:本文标题:javascript - Angular 2, set default value to select option - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1737367769a1825755.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论