Html 当我更新对象的字段时,对象数组不保留值

Html 当我更新对象的字段时,对象数组不保留值,html,angular,typescript,angular8,data-manipulation,Html,Angular,Typescript,Angular8,Data Manipulation,对象数组是一个表单数组,即产品:formBuilder.array([]) 我正在尝试更新一个对象。 有多个产品,用户可以选择更新数量。例如: product1.quantity :2 product2.quantity :5 当用户单击第一个产品的更新产品数量按钮时,数量更改为3,但当用户尝试更新产品2时。product2的数量更新为6,但product1的数量更改回2 更新产品数量1: product1.quantity:3 product2.quantity:5 更新数量产品2: pr

对象数组是一个表单数组,即产品:formBuilder.array([]) 我正在尝试更新一个对象。 有多个产品,用户可以选择更新数量。例如:

product1.quantity :2
product2.quantity :5
当用户单击第一个产品的更新产品数量按钮时,数量更改为3,但当用户尝试更新产品2时。product2的数量更新为6,但product1的数量更改回2

更新产品数量1:

product1.quantity:3
product2.quantity:5
更新数量产品2:

product1.quantity:2
product2.quantity:6
我无法理解这种行为。以下是供您参考的代码片段: .ts文件代码(单击按钮时调用的函数)

HTML代码

<div *ngFor="let product_details_tax of newrequest?.get('productsinvoice')['controls']; let i=index"
                                            [formGroupName]="i">
                                            <div class="row">
                                                <div class="form-group col formgroup_mar_bottom">
                                                    <label for="inputRate4">Product Name</label>
                                                    <input type="text" class="form-control" id="inputRate4"
                                                        formControlName='productname' placeholder="Enter Product Name">
                                                </div>
                                                <div class="form-group col formgroup_mar_bottom">
                                                    <label for="inputAmount">Quantity</label>
                                                    <input type="number" class="form-control" id="inputAmount4"
                                                        formControlName='quantity'
                                                        value={{product_details_tax.value?.quantity}}
                                                        placeholder="Enter Quantity">
                                                </div>

当我使用排列运算符时,数量甚至不会第一次得到更新。

尝试使用排列运算符更新产品对象

   this.productsinvoice.value.forEach(productInvoice => {
      if(productInvoice.productID === this.productID){
         productInvoice = {
             ...productInvoice,
             quantity: this.product_quantity
         }
      }   
    });

使用双向绑定,因此将
value={{{product\u detax.value?.quantity}}
更改为
[(value)]=“product\u detax.value?.quantity”

这样,您就不需要手动更改值,因为用户更改了模型应该更改的输入值


请参阅说明:

您可以创建一个临时数组并更新该数组中的值,然后调用setValue函数

let tempArr = productsInvoice
loop through tempArr to update your value
productsInvoice.setValue(tempArr)

使用.ts和.html文件会更容易一些。您是在使用数组还是对象?@ioedeveloper对象数组(产品)尝试使用扩展运算符更新对象product={…product,quantity:newQuantity}当我使用此代码时,产品甚至在第一时间都没有得到更新。我已经更新了答案。请使用更新的答案重试。对我无效。如果我将productInvoice记录在“If block”中,则会记录更新后的值。但当更新后的值再次记录到旧值时,对象数组是一个表单数组。这对我来说很有效,但我正在寻找更好的解决方案
   this.productsinvoice.value.forEach(productInvoice => {
      if(productInvoice.productID === this.productID){
         productInvoice = {
             ...productInvoice,
             quantity: this.product_quantity
         }
      }   
    });
let tempArr = productsInvoice
loop through tempArr to update your value
productsInvoice.setValue(tempArr)