-1

Execute function in other component without parent child relationship in angular 8. Demo code is given url https://stackblitz.com/edit/angular-bex9hp

sam
  • 1
  • 1
    Does 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 Answers1

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