1

Any time I enter a number into an input that is 2 way bound to my object the value is converted to a string.
How can I force it or convert it to be a number?

It seems like it should be very simple and yet I have struggled to find an elegant solution.

I saw this solution of using a function to convert it but I don't see how that would work for 2 way data binding.
convert string to number angular 2 one way binding

          <div class="row"> 
            <mat-form-field class="custom-control">
              <input type="number" matInput class="custom-control" [(ngModel)]="mpv.baseFare" required placeholder="Base Fare">
            </mat-form-field>
          </div>
          <div class="row"> 
            <mat-form-field class="custom-control">
              <input type="number" matInput class="custom-control" [(ngModel)]="mpv.mileageRate" required placeholder="Mileage Rate">
            </mat-form-field>
          </div>

mpv is an object of class VehicleType I have my VehicleType class set up as follows. The number types are completely ignored, I guess because it is at run time so just non typed javascript.

 export class VehicleType {
    baseFare: number;
    mileageRate: number;
}

UPDATE - CURRENT HACKY SOLUTION:

onSave(vehicleType: VehicleType) {
// Hack to convert string to number
vehicleType.bags = +vehicleType.bags;
vehicleType.baseFare = +vehicleType.baseFare;
vehicleType.mileageRate = +vehicleType.mileageRate;
vehicleType.passengers = +vehicleType.passengers;
this.vehicleTypeService.updateVehicleType(vehicleType.id, vehicleType).subscribe(result => {
5
  • How do you know it is being converted to a string? Commented Oct 16, 2019 at 23:01
  • It seems to work out of the box in this example: stackblitz.com/edit/angular-hkjr6v ? Commented Oct 16, 2019 at 23:13
  • It is definitely being converted to a string. I checked the type and it ends up in my firestore database as a string. Commented Oct 17, 2019 at 0:54
  • @Zze It appears to work with a normal input but fails with a angular material one as per my example. Commented Oct 17, 2019 at 1:03
  • Figured it. See answer. Commented Oct 17, 2019 at 1:11

2 Answers 2

0

It turns out that the order of the attributes on a matInput mater. This issue does not occur on a standard input only a matInput.

Does not work:

 <input type="number" matInput class="custom-control" [(ngModel)]="mpv.baseFare" required placeholder="Base Fare">

Works:

 <input matInput type="number" class="custom-control" [(ngModel)]="mpv.baseFare" required placeholder="Base Fare">
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to be able to modify the value when ngModel sets a new value then you're most likely looking at creating a get and set function. Example,

<div class="row"> 
  <mat-form-field class="custom-control">
    <input type="number" matInput class="custom-control" [(ngModel)]="baseFare" required placeholder="Base Fare">
  </mat-form-field>
</div>
<div class="row"> 
  <mat-form-field class="custom-control">
    <input type="number" matInput class="custom-control" [(ngModel)]="mileageRate" required placeholder="Mileage Rate">
  </mat-form-field>
</div>

get baseFare(): number {
  return mpv.baseFare;
}
set baseFare(value: any) {
  mpv.baseFare = pareseInt(value, 10);
}
get mileageRate(): number {
  return mpv.mileageRate;
}
set mileageRate(value: any) {
  mpv.mileageRate = pareseInt(value, 10);
}

That should do the job.

Update: you could just have value as any or both as any since you know the return type will be a number.

2 Comments

Error: 'get' and 'set' accessor must have the same type.
I found the answer. No need to change the class. See below. Thanks though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.