Yosef Buskila
Yosef Buskila

Reputation: 33

in Aurelia - I need to inject data from array of numbers, but it injected in first and no more

in Aurelia - I need to inject data from array of numbers, but it injected in first and no more.

see in example that the number of index 0 was injected well but when the number is change, the view is not update.

export class App {
  digits: number[]
  constructor() {
    this.digits = [1, 2, 3]
  }
  add() {
    this.digits[0]++
  }
}

<template>
  <p>digit: ${digits[0]}</p>
  <button click.trigger="add()">add</button>
</template>

Thank you all

Upvotes: 1

Views: 69

Answers (1)

avrahamcool
avrahamcool

Reputation: 14094

aurelia's strategy for observing arrays (in V1) doesnt work for index access. as stated in the docs

Aurelia will not be able to observe changes to arrays using the array[index] = value syntax. To ensure that Aurelia can observe the changes on your array, make use of the Array methods: Array.prototype.push, Array.prototype.pop and Array.prototype.splice.

AU2 uses proxies to patch arrays, so binding using direct indexing should also work.

a better way to bind the view to the first element only of an array, will be to use a getter like this: https://codesandbox.io/s/stoic-payne-okrzk?file=/src/app.html

but note that this is also not recommended - because getters without @computedFrom are re-computed 5 times per second.

Upvotes: 1

Related Questions