我想如果选择年份单选按钮,选择2018选择的操作值,现在没人选。
类代码是:
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>
如上面的角度5代码。
答案 0 :(得分:0)
如果更改“类型”,您必须说Angular会发生什么。因此,您必须“分离”ngModel和ngModelChange。如果使用简单的[(ngModel)],则只能更改变量“type”
<input type="radio" value="3" [ngModel]="type"
(ngModelChange)="type = 3;alls=yData[0].Year">
实现同样的其他方法是使用getter setter并使用[(ngModel)]
在你的.ts
_type:number;
get type(){
return _type;
}
set type(value:any)
{
this._type=value;
if (this._type==3)
this.alls=this.yData[0].Year
}
其他方式是使用Reactive Form并进行更改,请参阅https://angular.io/guide/reactive-forms#reactive-forms