EduBw
EduBw

Reputation: 942

I can't change values with ngModel

I am trying change the value in my service.year.ts but it is impossible ...

  <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";

I always get for console -> ***

Upvotes: 0

Views: 98

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

because all the options have the same value set for ngValue. change it. like,

 <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>

Upvotes: 1

Related Questions