单击按钮时如何将表名从第一个组件传递到角度7上的第二个组件? [英] How to pass table name from first component to second component on angular 7 when click button ?

查看:88
本文介绍了单击按钮时如何将表名从第一个组件传递到角度7上的第二个组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击按钮时如何将表名从第一个组件传递到角度7上的第二个组件?



我有两个组件而且我当第一个组件上的单击按钮时需要将表名从第一个组件传递到第二个组件





正如您在第一个组件中看到的那样我写表如果我有更多组件

,那么我将需要将表名称传递给下面的员工,从第一个组件到第二个组件.ts



我尝试过:



How to pass table name from first component to second component on angular 7 when click button ?

I have two components and i need to pass table name from first component to second when click button on first component


As you see below in first component i write table name by hand this will make big problems if i have more components
so that i need to pass table name as below Employees from first component.ts to second component .ts

What I have tried:

first component.ts

 <button class="btn btn-success"   (click)="getReferenceDataForEmp()">ReferenceFile</button> 
 getReferenceDataForEmp() {
  console.log('Reference fire');  
  this.router.navigate(['References']); 
   }
second component.ts

ngOnInit() {
    console.log('welcome');
     this.apiservice.getReferenceDataForEmployee('Employees')
    .subscribe( data=>{
     this.RefListVal = data; //SUBSCRIBE HERE
     console.log(this.RefListVal);

     });
on API service function getreferenceDataForEmployee
 getReferenceDataForEmployee(tablename : string):Observable<referencecolumns[]> {
                return this.http.get<referencecolumns[]>('https://localhost:44326/api/Reference/'+ tablename);
              }

推荐答案

有多种方法可以做到这一点。您可以使用父组件充当协调器,因此您可能在包含该表的组件中有一个EventEmitter,然后,当设置它时,它会引发父级正在侦听的事件。然后,父级将使用Input更新绑定。这是一种笨重的方式。



或者,您可以使用类似RxJS的东西创建一个为您处理此服务的服务,以便在表名称时通知其他组件已更新。
There are multiple ways that you could do this. You could use the parent component to act as a coordinator, so you might have an EventEmitter in the component containing the table and then, when this is set, it raises the event which is being listened to by the parent. The parent would then update the binding using Input. That's a clunky way of doing it.

Alternatively, you could just create a service that handled this for you using something like RxJS to notify the other component when the table name is updated.


这篇关于单击按钮时如何将表名从第一个组件传递到角度7上的第二个组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆