-2
In my component I have 2 objects that use the same model, as below:
Component:
produtos: Produto[] = [];
produtosOriginal: Produto[] = [];
At a given time I make a copy of the values:
this.produtosOriginal = this.produtos;
In HTML, I do a for and link input to property:
HTML:
<ng-container *ngFor="let produto of produtos; let i = index;">
...
<input class="form-control form-control-sm"
type="number"
min="1"
step="1"
(focusout)="validarItem(produto.id)"
(input)="validarItem(produto.id)"
[ngClass]="{ 'is-invalid': produto.erroQtde }"
[(ngModel)]="produto.qtde" />
</ng-container>
When I make a change to the input the value is passed to the 2 objects, I have already checked if this value is being passed by another factor, more surely the problem is with the [(ngModel)]. How can I update only the object products.
Observing:
- I’m using Angular 8.
- I have tried using [(ngModel)]="products[i]. value" but no result.
The list
produtos
is a copy ofprodutosOriginal
? if it is you will have to copy one to another using spread (...
). That would be assimilation:produtos = {...produtosOriginal};
so the change in "products" does not reflect in "products".– Leonardo Getulio
I could not understand very well, because there are 2 different variables with the same type of object in the same component?
– LeAndrade
If you are populating
produtos
through theprodutosOriginal
as follows:this.produtos = this.produtosOriginal;
you are not passing the values, you are passing the reference. Soonprodutos
points toprodutosOriginal
, and any change inprodutos
will changeprodutosOriginal
. I hope you can understand...– Lucas Ayrosa
Leonardo Getulio, I tried to do as you did, but for my logic it would be the opposite: this.productsOriginal = { ... this.products }; only that I can no longer access the methods for example: this.productsOriginal.length
– Alessandro
Lucas Ayrosa, what would then be the correct way to popular a copy?
– Alessandro
The way @Leonardogetulio really said it, only I believe the only mistake was the keys... It would be:
this.produtosOriginal = [...this.produtos];
. Test and see if it will solve your problem.– Lucas Ayrosa
this.productsOriginal = [...this.products] = continues to reflect / this.productsOriginal = {...this.products} = lose methods of "productsOriginal"
– Alessandro
Method or property? If it is method it is likely that you will lose even. If it is property/value it should not lose, this function is native, however it can scan the array and give . push() on each object but assimilate in a constant. Example:
const copiaProduto = {...produtosOriginal[0]}; produtos.push(copiaProduto);
. Forehead there, something calls here.– Leonardo Getulio
Do you mean that you keep modifying the two arrays? Because it should not happen. You can quickly test in the browser console by creating an example array, copying this one to another the way you were doing
a = b
and create a third withc = [...a]
, modifies the values ofb
andc
and will see that only the change ofb
influence ona
. Anyway, there’s another way to copy values:this.produtosOriginal = this.produtos.slice();
. You can test it if you want.– Lucas Ayrosa
If I use this.productsOriginal = { ...this.products } I lose the methods . find(), filter(), etc.. / If I use this.productsOriginal = [ ...this.products ] everything I change in products reflects in productsOriginal. About being working on the console really works, maybe it’s something with ngModel, I honestly don’t know.
– Alessandro