developer
developer

Reputation: 1687

Call App.component.ts method from another component

I have a "signup" method on app.component.ts class which is being called from "Sign up" link on app.component.html file. It internally calls a service class and some more login in it.

html:

<a (click)="signup($event)">Sign up</a>

ts:

export class AppComponent implements OnInit, OnDestroy {
       signup(e: any) {
            this.service.signup();
       }

}

There is another html/component file which has the same signup link. Is it possible to call AppComponent's signup method directly from the html/component file?

Upvotes: 0

Views: 964

Answers (1)

You can call one component method/function in below conditions

  • Both component should be in same DOM mean both have parent child relation.
  • You can shared main component across other component
  • Make shared service.
  • Suscribe event on any property/value change. Event Emitter.

Better to use shared service for your requirement, where common function are together.

Upvotes: 1

Related Questions