8

convert the string to number angular 2 one way binding

[longitude]="loc.location.lng">

loc.location.lng is string i need to convert them to number.but still not working as I expect. In interpolation it's working but in this not working. Thanks

2

3 Answers 3

14

You could also force the cast by trying this:

[longitude]="+loc.location.lng"
Sign up to request clarification or add additional context in comments.

1 Comment

What about it if the value of loc.location.lng is null?
6

Make a function which convert the value to float as its location and return the value.

    ConvertString(value){
return parseFloat(value)
}

Call the function in template

[longitude]="ConvertString(loc.location.lng)"

3 Comments

Unlikely that it would work with the typo Covert <=> Convert. I'm also not sure what the [longitude]={...} syntax does. What is the meaning of the curly brackets? And you're missing a right paren. Finally, is it really that much trouble to properly format and indent your code?
Updated u can check it was typo mistake
Do not use this approach, use a custom pipe instead. You should never use function calls in Angular template Why you should never use function calls in Angular template expressions
4

adii's answer is not good in terms of performance since change detection will cause the method to run several times .. consider using a pipe.

Comments

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.