【问题标题】:How to push/pass table data from one component to another in angular 9?如何以角度 9 将表格数据从一个组件推送/传递到另一个组件?
【发布时间】:2020-12-25 15:48:13
【问题描述】:

当我单击按钮 (pushData()) 时,我想将表 (itemName、Quantity、retailRate 和 totalQuantity、totalPrice) 的数据推送/传递到另一个组件(getDataComponent) 也导航以接收。 我无法在控制器中发送表数据。这种方法是否适合通过导航将数据从一个组件发送到另一个组件...

HTML: * send.html*

        <table class="table table-hover table-sm"
          *ngIf="secondTableData.length">
           <thead>
             <tr class="table-primary">
               <td># </td>
               <td>Item</td>
               <td>Quantity</td>
               <td>Price</td>
               <td class="pull-right">Action</td>
           </tr>
       </thead>
       <tbody>
            <tr *ngFor="let newdata of secondTableData let i=index;">
               <td>{{i+1}}</td>           
               <td>{{ newdata.itemName }}</td>  <!--this data to push in receiveComponent -->
               <td class="text-center">{{ newdata.Quantity }}</td>   <!--this data to push in receiveComponent -->
               <td>{{ newdata.retailRate }}</td>  <!--this data to push in receiveComponent -->
              
            </tr>
        </tbody>
        <thead>
           <tr class="table-secondary">
               <td>#: {{ secondTableData.length }}</td> 
               <td></td>
               <td class="text-center">TQ: {{  totalQuantity }}</td>   <!--this data to push in receiveComponent -->
               <td>TP: {{ totalPrice }}</td>   <!--this data to push in receiveComponent -->
               <td></td>
            </tr>
        </thead>
      </table>
     <div>
       <hr>
       <button class="btn btn-primary pull-right"  (click)="pushData()">
           <i class="fa fa-check" aria-hidden="true"></i> OK
       </button>
      
     </div>
   </div>```

**Component.ts:** *sendComponent.ts*

   ```tabeTata:any 
       pushData(){
        let data:any = this.tabeTata.values;
        this.routerService.navigate(['./receive'],{
           queryParams:{data:JSON.stringify(data)}
        })
       }```

**receive.Html**  *here i am using random data for demo. I want to place received data to respective places*

   ```<table class="table table-bordered table-sm">
       <thead>
         <tr>
           <th>SN</th>
           <th>Product</th>
           <th>Unit</th>
           <th>Price</th>
           <th>Total</th>
         </tr>
       </thead>
       <tbody>
        <tr>
           <td>1</td>
           <td>Custom oil </td>
           <td>10</td>
           <td>34</td>
           <td>340</td>
        </tr>
         <tr>
           <td>2</td>
           <td>Digital illustraion </td>
           <td>12</td>
           <td>50</td>
           <td>600</td>
         </tr>
           <tr class="small" style="height: 10px;">
               <td colspan="3" style="border: none;"></td>
               <td>Gross Amount:</td>
               <td>1100</td>
           </tr>
           <tr class="small">
               <td colspan="3" style="border: none;"></td>
               <td>Discount:</td>
               <td>100</td>
           </tr>
           <tr class="small">
               <td colspan="3" style="border: none;"></td>
               <td>VAT</td>
               <td>30</td>
           </tr>
           <tr class="small">
               <td colspan="3" style="border: none;"></td>
               <td>Net Amont</td>
               <td>1030</td>
           </tr>
       </tbody>
    </table>
   ```

**receivecomponent.ts**

   ```data:any;
      ngOnInit(): void {
      // To get data from biling component
       this.route.queryParams.subscribe((params)=>{
        this.data = JSON.parse(params.data);
        console.log(params);
      })
    }```


**Any one is there to help, May be this one is complex**

【问题讨论】:

  • 这能回答你的问题吗? Data sending between components doesn't work
  • 这不是我想要的
  • 然后在路由器中发送带有状态的数据
  • 你能请stackblitz吗!!..有没有办法将表的数据传递给控制器​​?
  • @Sivakumar Tadisetti 您对此有什么解决方案吗? :-)

标签: javascript angular typescript angular-directive angular9


【解决方案1】:

尝试在组件之间使用sharedService

这是一个通过服务实现“组件互通”的示例:

https://stackblitz.com/edit/angular-communication-3?file=app%2Fside-bar%2Fside-bar.service.ts

更多信息请转这篇文章'3 ways to communicate between Angular components'

【讨论】:

  • 这不是我想要的。
  • 你似乎想要的 Dany 并不是它在 Angular 中的实现方式。在 Angular 中,数据使用服务在组件之间进行通信。
  • 可以在控制器中传递Table()的数据吗?如果是,那么如何?
  • Angular 不以 HTML 为中心,您的程序中不应该有“表格数据 ()”的概念。您有一个 Component 类,它应该包含您在表中显示的数据。 HTML 仅用于显示内容并生成传递给组件的用户事件。然后,您使用服务将该数据传递给另一个组件。
  • 您可以将component.secondTableData 传递给服务,并从其他组件读取该数据。
猜你喜欢
相关资源
最近更新 更多
热门标签