A single Arrayobject for presentation and modification

Asked

Viewed 32 times

0

Hello, I’m venturing into Angular 8, and I came across a stalemate, I get the following obj array of a service:

this.productsService.getProducts().subscribe(
      (products: Product[]) => {
         this.productsToShow = products;
         this.productsToEdit = products;
         // ja tentei fazer = [...products], etc
      });

and whenever I edit the object form productsToEdit, automatically the display grid is modified, in the grid there is a ngfor and being exhibited in the conventional way {{ element.atributo }}, there is some way to "freeze" the display?

  • 1

    It was not at all clear your question!

1 answer

1


The problem seems to be that you’re copying the same reference, try so:

this.productsToShow = {...products};
this.productsToEdit =Object.assign({}, products);

Or to have totally another object:

 this.productsToShow =JSON.parse(JSON.stringify(products));
  • was exactly that, I was researching how javascript works, and learning more every day with it, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.