Angular 4 - Execute a function from outside a component

I would be grateful for your suggestions in the following case: I have a registration component. Our product manager decided to add this registration component to another page. On this page, another button (outside the registration component) must complete the registration (along with other logic) I do not want the new page to process the registration code, and it will be great if I can still have all the logic in the registration component. What should I do?

-2
source share
1 answer

Two ways:

  • Put the function in a shared service and execute from there
  • Get the component object and call the function from there:

Let's say your component name is "TestComponent", then:

Add #test selector id to your selector in test.component.html

<test #test></test>

Get the TestComponent in your component where you want to call the function:

 @ViewChild('test') private testComponent: TestComponent; /* Remember to import the TestComponent in the file where you get this */ 

Call the method you want to execute. This method should be publicly available in your TestComponent. For instance.

this.testComponent.registerUser ();

0
source

Source: https://habr.com/ru/post/1270446/


All Articles