IamGrooot
IamGrooot

Reputation: 1040

How to cast string to number in angular template

I have a select box from which I am binding the selected value to startingYear variable. I want type of startingYear to be in number. But its always in string.

How can I cast it to number?

console.log(StartingYear);
<select  [(ngModel)]="StartingYear">  
    <option *ngFor="let item of [0,1,2,3,4]; let i = index">
                      {{i}}
     </option>
 </select>

Upvotes: 1

Views: 3162

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

The value property would be a string by default, so use ngValue property to the option tag. Unlike the value binding, ngValue supports binding to objects.

<select  [(ngModel)]="StartingYear">  
  <option *ngFor="let item of [0,1,2,3,4]; let i = index" [ngValue]="i">
        {{i}}
  </option>
</select>

Upvotes: 2

Related Questions