Home > Software engineering >  In Angular, how do I run a method in a different component than the current component
In Angular, how do I run a method in a different component than the current component

Time:09-17

How do I run a method in component Y from component X in Angular.

method in X component

  onClear() {
    this.editor.clear();
    // environment.templateId=0;
    this.openScheduler();
  }

like so Y component


 X.onclear();

CodePudding user response:

Use a service to interact between components or to use shared methods.

CodePudding user response:

If it's parent-child component relation you can do this like this.

@ViewChild('componentName', { static: false })
  component: SomeComponent;

And in template:

<component #componentName></component>

Than you can just:

  onClear() {
    this.editor.clear();
    // environment.templateId=0;
    this.component.doSomehting();
  }
  • Related