简体   繁体   English

两个组件之间的角度通讯

[英]Angular communication between two components

I'm building a simple web based application using angular version 6. 我正在使用角度版本6构建一个简单的基于Web的应用程序。

In my application there is a component which has a child component. 在我的应用程序中,有一个包含子组件的组件。 There is a function in this component(In the parent component, not the child component.) and I want to invoke that function using a button which is in the child component. 该组件中有一个函数(在父组件中,而不是子组件。),我想使用子组件中的按钮来调用该函数。

This image explains the format of my components. 此图像说明了我的组件的格式。

在此处输入图片说明

I think its regarding to angular @Output . 我认为它与angular @Output有关。 But i can't manage it. 但是我管理不了。

This is how my code has organized. 这就是我的代码的组织方式。

Parent Component - component.ts file 父组件-component.ts文件

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-teacher-home',
  templateUrl: './teacher-home.component.html',
  styleUrls: ['./teacher-home.component.scss']
})
export class TeacherHomeComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  formView: boolean = false;

  toggleForm(){
    this.formView = !this.formView;
  }
}

Parent component - component.html file 父组件-component.html文件

<div>
    <child-compnent></child-compnent>
</div>

Child component - component.html file 子组件-component.html文件

<div>
    <button>Toggle Form view</button>
</div>

i want to callthe function toggleForm() of parent component when the button clicked which is in child component. 我想在子组件中单击按钮时toggleForm()父组件的功能toggleForm()

read this article: Understanding @Output and EventEmitter in Angular 阅读本文: 了解Angular中的@Output和EventEmitter

child component: 子组件:

@Component({
  selector: 'app-child',
  template: `<button (click)="sendToParent('hi')" >sendToParent</button> `
})
export class AppChildComponent {
  @Output() childToParent = new EventEmitter;

  sendToParent(name){
    this.childToParent.emit(name);
  }
}

parent component: 父组件:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  toggle(){
    console.log('toggle')
  }

}

parent html: 父html:

<app-child (childToParent)="toggle($event)"></app-child>

working DEMO 工作演示

You have a couple of ways to do this : 您有两种方法可以做到这一点:

  1. Is to create an event inside the child component and then give it a callback, something like this: 是在子组件内部创建一个事件,然后为其提供回调,如下所示:

    @Output('eventName') buttonPressed = new EventEmitter();

and call buttonPressed.emit() when you want the event to be triggered 并希望触发事件时调用buttonPressed.emit()

on the parent side it will look like this : 在父端,它将如下所示:

<div>
    <child-compnent (eventName)="toggleForm()"></child-compnent>
</div>
  1. Another way is to create a shared service that will contain the shared functions and data for both components 另一种方法是创建一个共享服务,其中将包含两个组件的共享功能和数据

You need to use @Output decorator inside your child component and emit an event when the button present clicked inside your child. 您需要在子组件内部使用@Output装饰器,并在孩子内部单击当前按钮时发出事件。

For eg: - 例如:-

Child component.html 子component.html

<div>
    <button (click)="childButtonClicked()">Toggle Form view</button>
</div>

Child component.ts 子组件

export class ChildComponent {
  @Output triggerToggle: EventEmitter<any> = new EventEmitter<any>();

  ...
   childButtonClicked() {
     this.triggerToggle.emit(true);
   }
  ...
}

Parent Component 父组件

<div>
    <child-compnent (triggerToggle)="toggleForm()"></child-compnent>
</div>

You can use EventEmitter of angular to listen to events from your child component. 您可以使用EventEmitter侦听来自子组件的事件。

parent.component.ts parent.component.ts

toggleForm($event) {} 

parent.component.html parent.component.html

<div>
    <child-compnent  (trigger)="toggleForm($event)" ></child-compnent>
</div>

child.component.ts child.component.ts

@Output() trigger : EventEmitter<any> = new EventEmitter<any>();

buttonClick(){
  this.trigger.emit('click');
}

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

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