我正在尝试更改service.year.ts中的值,但这是不可能的...
<select name="selectedyear" [disabled]="disabledPeriod" [(ngModel)]="selectedyear" (ngModelChange)="onChangeYear()">
<option [ngValue]="selectedyear" selected="true" disabled="true">Seleccione un año</option>
<option [ngValue]="selectedyear" name="selectedyear">2018</option>
<option [ngValue]="selectedyear" name="selectedyear">2017</option>
</select>
component.ts
public selectedyear: string = "Seleccione un año";
public onChangeYear = function () {
this.service.year = this.selectedyear;
if(this.service.year === "Seleccione un año"){
console.log("************ ");
}else {
console.log("this.service.year");
}
}
service.ts->
public year: string = "Seleccione un año";
我总是得到控制台-> ***
答案 0 :(得分:1)
因为所有选项都为ngValue
设置了相同的值。更改。喜欢,
<option [ngValue]="selectedyear" selected="true" disabled="true">Seleccione un año</option>
<option [ngValue]="'2018'" name="selectedyear">2018</option>
<option [ngValue]="'2017'" name="selectedyear">2017</option>