Angular2 - 从另一个独立组件调用函数.基本上,从外部组件调用函数 [英] Angular2 - call a function from another standalone component. Basically, call function from outside component

查看:31
本文介绍了Angular2 - 从另一个独立组件调用函数.基本上,从外部组件调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个组件 (Component1) 中加载另一个包含表单的独立组件 (Component2).

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)

主要组件(Component1)也有一个提交按钮.我想要的是从 Component2 中删除提交按钮并在我从 Component1 中单击提交时触发提交功能.

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.

类似于:

<button type="button" class="submit" (click)="saveData()">{{ Save }}</button>

saveData() {
     Component2.submitData(data);
}

ViewChild 将无法工作,因为这 2 个组件不是同一组件的一部分.此外,输出将不起作用.

ViewChild won't work since the 2 components are not part of the same component. Also, output won't work.

从 Component2 外部调用该函数有哪些选项?

What options do I have to call the function from outside Component2?

希望能帮到你.

谢谢.

问候,AG

推荐答案

这是关于组件交互的,你需要使用服务在两个不同的组件之间进行通信.主要是Angular的依赖注入.

This is about the component interaction, you need to use service for communicating between two different components. Main thing is the dependency injection of Angular.

您只需使用主题即可.

这篇关于Angular2 - 从另一个独立组件调用函数.基本上,从外部组件调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆