Search code examples
angulartypescriptngmodel

How to control the type of value bound using ngModel (number gets distorted to string)?


I asked a question and got it working. However, I notice that although the data provided consists of objects declared like this:

export interface GisPoint { e: number; n: number; }

when the user enters a value, the original contents being { e: 12, n: 34 } become string'ish, i.e. { e: "123", n: 34 }. I understand that the result from the input will be assumed a string so I'll have to convert it. However, as the binding goes through ngModel, I actually don't react explicitly to keyUp or blur.

Do I have to explicitly do that? If so, there's really no point using ngModel. It would be quite nifty to be able to keep the type when the edited value gets entered.


Solution

  • You can implement the ControlValueAccessor interface for the Component, so that you specify which value represents the model.

    Here's a couple good articles to start from: