简体   繁体   English

如何以角度 9 将表数据从一个组件推送/传递到另一个组件?

[英]How to push/pass table data from one component to another in angular 9?

I want to push/pass data of table (itemName, Quantity, retailRate and totalQuantity , totalPrice ) to another component(getDataComponent) when I click button (pushData()) also navigate to receive.当我单击按钮 (pushData()) 时,我想将表的数据(itemName、Quantity、retailRate 和 totalQuantity、totalPrice)推送/传递给另一个组件(getDataComponent)也导航到接收。 I am not able to send table data in controller.我无法在控制器中发送表数据。 Is this approach is good to send data from one component to another with navigation...这种方法是否适合通过导航将数据从一个组件发送到另一个组件...

HTML: * send.html* HTML: * 发送.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**

try using sharedService between components.尝试在组件之间使用sharedService

here is an example of 'Component Intercommunication' through services:这是通过服务进行“组件互通”的示例:

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

For more information resd this article '3 ways to communicate between Angular components'有关更多信息,请参阅本文“在 Angular 组件之间进行通信的 3 种方法”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以角度将数据从一个组件传递到另一个组件 - Pass data from one component to another in angular 将数据从一个Angular组件传递到另一个 - Pass data from one Angular component to another 如何将数据从一个组件传递到另一个 angular2 - How to Pass the data from one component to another angular2 如何将数据从一个组件传递到另一个组件? - How to pass data from one component to another component? 如何在reactjs中将数据数组从一个组件传递到另一个组件 - How to pass array of data from one component to another component in reactjs 如何将数据输入从一个组件传递到另一个组件? - How to pass the data input from one component into another component? 如何以角度将数据从一个组件传递到另一个组件(新浏览器选项卡)? - How do I pass data from one component to another (New Browser Tab) in angular? 如何在component.ts中将数据从一个函数传递到另一个函数,angular2 - how to pass data from one function to another in component.ts , angular2 如何将带有 id 的数组从一个组件传递到另一个组件(角度) - How to pass an array with id's from one component to another (angular) 如何使用Angular JS将数据从一个div推到另一个div(表)? - How to push data from one div to another div(table) using angular js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM