And component code: private _value:...">

Angular 2 @ Error entering number

I have the following component:

<component value="3"></component>

And component code:

  private _value:number;

  get value(): number {
    return this._value;
  }

  @Input()
  set value(value: number) {
    console.log(value);
    console.log(typeof value);
    this._value = value;
  }

Magazine:

3
string

But if I bind a property like:

<component [value]="variable1"></component>

In this case, I get a number if variable1 is a type number.

3
number

I know there is no magic with typescript, but is this true? Should Angular input decoder do the conversion?

I check types in setters, but I get errors when compiling typescript.

I do not want to use the any type in gettes and setter.

Any elegant solution?

+4
source share
1 answer

When binding with brackets, the []value is bound directly to the object.

When binding attributes, the value between quotation marks is treated as a string.

, .


Plunker, ,

+4

Source: https://habr.com/ru/post/1656555/


All Articles