I'm loading in a component (Component1) another standalone component(Component2) that contains a form.
Imagine you have made a form component and I want to integrate it in my component and trigger the submit function you have written, without clicking on your submit Button.
The submit button on this form triggers a function to save data:
submitData(newUserForm: NgForm)
The main component (Component1) also has a submit button. What I want is to remove the submit button from Component2 and trigger the submit function when I click on submit from Component1.
Something like:
<button type="button" class="submit" (click)="saveData()">{{ Save }}</button>
saveData() {
Component2.submitData(data);
}
ViewChild won't work since the 2 components are not part of the same component. Also, output won't work.
What options do I have to call the function from outside Component2?
I hope you can help.
Thank you.
Regards, AG
Without much context, I think you need a service to do this. If there's no relation between those 2 components, why would you call a function from component 2? Put save logic into a service and inject it on whatever component you need it.