Execute function in other component without parent child relationship in angular 8. Demo code is given url https://stackblitz.com/edit/angular-bex9hp
Asked
Active
Viewed 414 times
-1
-
1Does this answer your question? [How to call another components function in angular2](https://stackoverflow.com/questions/37587732/how-to-call-another-components-function-in-angular2) – Dinesh Shekhawat Feb 20 '20 at 10:06
-
Input Output not helpful as no connection between two component, so above link is not helpful – sam Feb 20 '20 at 10:08
-
Please look at the complete answer before jumping to conclusions. In the last section you will see how two components can interact via services for unrelated components. – Dinesh Shekhawat Feb 20 '20 at 10:09
-
If possible please go through the link I have mentioined to understand – sam Feb 20 '20 at 10:13
-
Where should the `TwoComponent` be rendered? What's its place in DOM hierarchy? Also: you have two components with the same selector, `
`. – mbojko Feb 20 '20 at 10:20 -
my-app duplicate was a mistake, I want to click func2 from TwoComponent when click on proceed button – sam Feb 20 '20 at 10:23
-
If you don't intend to put `TwoComponent` somewhere inside your `AppComponent`, then, first, using `ViewChild` is a mistake, and two, use a service, not an another component. – mbojko Feb 20 '20 at 10:28
1 Answers
0
You have to declare the TwoComponent Reference anyway because if it is not constructed any of its functions will not be called.
change app.component.html
<my-app-two #TwoComponent></my-app-two>
<button type="button" class="btn btn-primary" (click)="func1()">Proceed</button>
And in TS file change @ViewChild
@ViewChild('TwoComponent', {static: false}) TwoComponent: TwoComponent;

Dinesh Shekhawat
- 504
- 6
- 13
-
-
1@Dinesh Shekhawat thanks, but in your example there is parent child relationship – sam Feb 20 '20 at 10:30
-
Like I said even if you avoid a parent child relationship and try to perform a sibling-sibling relationship the component declaration has to be given in the html – Dinesh Shekhawat Feb 20 '20 at 10:31
-
I was trying with @ViewChild but it is not helpful, Can u help if it is possible with observable – sam Feb 20 '20 at 10:31
-
Please update your code with the changes I have suggested and then see if it's working. I have tested the same on your stackblitz example. – Dinesh Shekhawat Feb 20 '20 at 10:32