简体   繁体   English

Angular2 - 从另一个独立组件调用 function。 基本上,从外部组件调用 function

[英]Angular2 - call a function from another standalone component. Basically, call function from outside component

I'm loading in a component (Component1) another standalone component(Component2) that contains a form.我正在一个组件(Component1)中加载另一个包含表单的独立组件(Component2)。

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.想象一下,你做了一个表单组件,我想把它集成到我的组件中,并触发你写的提交 function,而不需要点击你的提交按钮。

The submit button on this form triggers a function to save data:此表单上的提交按钮会触发 function 来保存数据:

submitData(newUserForm: NgForm)

The main component (Component1) also has a submit button.主组件(Component1)也有一个提交按钮。 What I want is to remove the submit button from Component2 and trigger the submit function when I click on submit from Component1.我想要的是从 Component2 中删除提交按钮,并在单击 Component1 中的提交时触发提交 function。

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. ViewChild 不起作用,因为这两个组件不是同一个组件的一部分。 Also, output won't work.此外,output 也不起作用。

What options do I have to call the function from outside Component2?我必须从 Component2 外部调用 function 有哪些选项?

I hope you can help.我希望你能帮忙。

Thank you.谢谢你。

Regards, AG问候, 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?如果这两个组件之间没有关系,为什么要从组件 2 调用 function? Put save logic into a service and inject it on whatever component you need it.将保存逻辑放入服务中并将其注入您需要的任何组件中。

Use a service here在此处使用服务

SomeService一些服务

private value: Subject<string> = new Subject<string>();

// return the Subject as an Observable in order to be able to subscribe to it
public get theValue(): Observable<string> {
    return this.value.asObservable();
}

// set the current value
public set theValue(value: string) {
    this.value.next(value);
}

Component 2 TS组件 2 TS

constructor(private someService: SomeService){

}

ngOnInit(): void {
    // subscribe to the value and refresh the local variable whenever it changes
    this.someService.theValue.subscribe( value => {
        console.log('Info from Component 1: ' + value;

        if (value === 'Saved') {
            Component2.submitData(data);
        }
    });
}

Component 1 TS组件 1 TS

constructor(private someService: SomeService){
}

saveData(): void {
    this.someService.theValue = 'Saved';
}

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.主要是Angular的依赖注入。

Simply you can use Subject.只需您可以使用主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM