【问题标题】:How to get td value and push,pop to an array in angular 8如何获取 td 值并推送、弹出到角度 8 的数组
【发布时间】:2020-05-22 17:11:05
【问题描述】:

我正在尝试选中复选框的行产品名称并推送一个数组,然后如果我取消选中我想从数组中删除产品名称。最后在一个数组中检查了复选框行的产品名称。我试过但没有工作。 请帮助任何人。

演示:https://stackblitz.com/edit/angular-b6urvc?file=src/app/app.component.ts

app.component.ts:

 getProoduct(event){

 if(event.target.checked)
   {
    const target = e.originalEvent.toElement.closest('tr');

     let tdProduct= target.querySelector('td:nth-child(3)').innetText;
    productArr.push(tdProduct); 
   }else{
    const target = e.originalEvent.toElement.closest('tr');

    let tdProductRemove = target.querySelector('td:nth-child(3)').innetText;
    productArr.pop(tdProductRemove);  
   }

   console.log(productArr); 
   }

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    只需在事件处理程序中传递产品,而不是根据 dom 进行复杂的操作来识别产品。

    component.ts

    getProduct(e, product){
    
     if(e.target.checked)
     {
        this.productArr.push(product.product_name); 
     }
     else
     {
        this.productArr.splice(this.productArr.indexOf(product.product_name), 1);  
     }
    

    component.html

    <td> <input type="checkbox" (change)="getProduct($event, data)"> 
    

    Stackbliz demo

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 2017-12-03
      相关资源
      最近更新 更多