【发布时间】:2017-02-05 01:20:29
【问题描述】:
我尝试为选项添加默认值。它就像一种占位符,我使用this method 来完成它。
在纯 HTML 中它可以工作,但如果我尝试使用 angular 2 属性的 *ngFor,它不会选择任何东西。
这是我在纯 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>
并使用 Angular 模板:
<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>
班级是
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
}
]
}
}
我希望Select Language被选为默认值。它被禁用但未被选中。
如何选择它作为默认值?
【问题讨论】:
标签: javascript angular angular2-forms