1

I'm trying to render an array of strings in a template with double curly braces.

Additionally I have a button that, when clicked, adds another string to the array.

Why is {{ myarray }} never updated after clicking the button, but {{ myarray.length }} is?

I did a codepen here.

3
  • 1
    I’m surprised the initial value renders. Try this: <p>{{ myarray | json }}</p> Commented Feb 9, 2021 at 18:20
  • @MikeOne hah, that works. How come, surprised? Commented Feb 9, 2021 at 18:22
  • Because I would have expected it to print [Object object].. Commented Feb 9, 2021 at 18:26

1 Answer 1

1

What you can do here is to use Object's prototype function toString() (even though MikeOne comment is great catch, usually json() is useful for debugging. And it will print you array with brackets).

toString() and valueOf() are two methods on Object's prototype, used to convert values to primitive types (and {{ }} does exactly this - tries to convert your array into a primitive value. So it did render initial values, because toString() was called):

{{ myarray.toString() }} 

And it prints array length change, because thats a primitive value.

Can read more about toString() and valueOf() here

Sign up to request clarification or add additional context in comments.

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.