George Perid
George Perid

Reputation: 87

Mat-option cannot be converted to number

I want to display a set of numbers in a menu and I use mat-select/mat-option. I have a validator that checks if the values of the menu are numbers (since the user can either select a number from the menu or insert one from the keyboard).

I try to convert these values to numbers, before displaying them on the template as follows:

 nodes.forEach((node) => {
      console.log(typeof(node.x)); //string
      console.log(node.x); //10.0

      node.x = parseInt(node.x);
      console.log(typeof(node.x), node.x); //undefined, undefined

      this.options.push(node);
});

Why the values cannot be converted from strings to integers? Also, why if I display them to the template without trying to convert them to numbers, the validator returns an error that the option is not a number?

<mat-option *ngFor="let node of options">
      {{ node["x"]}}
</mat-option>

Upvotes: 0

Views: 62

Answers (0)

Related Questions