【发布时间】:2018-08-07 22:08:24
【问题描述】:
如果我选择年份单选按钮,我想选择 opeion value 2018 selected,现在没有选择。
类代码是:
alls:string='';
type:string ='1';
yData = [{"Year":2018},{"Year":2017},{"Year":2016}]`enter code here`
这是模板代码:
<div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="1">All Search</label>
</div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="2">Album Title </label>
</div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="3">Year </label>
</div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="4">Event Date </label>
</div>
<div class="radio">
<label><input type="radio" [(ngModel)]="type" name="type" value="5">Description </label>
</div>
</div>
<div *ngIf="type === '1'">All Search:<input type="text" [(ngModel)]="alls" /><br></div>
<div *ngIf="type === '2'">Album Title: {{this.type}} <input type="text" [(ngModel)]="alls" /></div>
<div *ngIf="type === '3'">Year: {{this.type}} alls {{yData[0]["Year"]}}
<select [(ngModel)]="alls">
<option *ngFor="let yrow of yData" [value]="yrow.Year"> {{yrow.Year}} </option>
</select>
</div>
<div *ngIf="type === '4'">Event Date: {{this.type}} <input type="date" [(ngModel)]="alls" /></div>
<div *ngIf="type === '5'">Description: {{this.type}} <input type="text" [(ngModel)]="alls" /></div>
如上 Angular 5 代码。
【问题讨论】:
-
如果您选择 2018 选项?那么你期望发生什么?
-
好吧,你的选择的 ngModel(保存选择的值)是
alls。它的值为''。为什么会选择 2018 年? -
先生,有两个块,一个用于单选按钮,另一个用于显示输入控件,这与单选按钮相关,例如如果 'type' 值等于 3 则将显示年份选择控件,即工作完美,但在选择控件中数据来自 yData,(来自 http.get().subscribe()),但我需要在选择选项中默认值应该是 2018(第一个索引)。我希望你明白我需要什么。
标签: angular angular2-forms angular5