Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
              }
Posted
Updated 20-May-19 21:47pm
v2

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900