Detailed explanation of angular parent-child component communication

Detailed explanation of angular parent-child component communication

APIs used

Input - defines acceptable properties in the child component, which can be used by the parent component to pass data to the child component

Output - The output property of the child component. This property needs to be of EventEmitter event type to notify the parent component to take corresponding actions.

EventEmitter - used in components with @Output directive to emit custom events synchronously or asynchronously and register handlers for these events by subscribing to instances.

Simple Example

Render subcomponents in a list, click on a subcomponent to notify the parent component to perform an operation

person.ts

export interface Person {
  name: string;
  age: number;
  sex: string;
}

Parent Component

import { Component, OnInit } from '@angular/core';
import { Person } from './person';
@Component({
  selector: 'app-comp-parent',
  template: `
    <app-comp-child
      *ngFor="let person of personList"
      (itemClick)="onItemClick($event)"
      [data]="person"
    ></app-comp-child>
  `,
})
export class CompParentComponent implements OnInit {
  personList: Person[] = [
    { name: '张三', age: 21, sex: '男' },
    { name: 'Li Si', age: 25, sex: 'Male' },
    { name: '李莉', age: 20, sex: '女' },
  ];
  constructor(){ }
  ngOnInit(): void { }
  onItemClick(item: Person){
    console.log('click-person: ', item);
  }
}

Subcomponents

import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Person } from './person';
@Component({
  selector: 'app-comp-child',
  template: `
    <div (click)="itemClick.emit(data)">
      Name: {{ data.name }}
      Age: {{ data.age }}
      Sex: {{ data.sex }}
    </div>
  `,
})
export class CompChildComponent implements OnInit {
  @Input() data!: Person;
  @Output() itemClick = new EventEmitter();
  constructor(){ }
  ngOnInit(): void { }
}

Effect

Rendering

Summarize

This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Example of Angular2 parent-child component communication
  • Implementation of shared service communication between Angular 2 parent and child components
  • Angular2 parent-child component data communication example

<<:  Pure CSS code to achieve drag effect

>>:  A detailed introduction to Linux memory management and addressing

Recommend

Detailed explanation of Docker compose orchestration tool

Docker Compose Docker Compose is a tool for defin...

JavaScript canvas to achieve colorful clock effect

Use canvas to write a colorful clock! 1. Title (1...

Example of how to achieve ceiling effect using WeChat applet

Table of contents 1. Implementation 2. Problems 3...

JS realizes automatic playback of timeline

Recently, I have implemented such an effect: clic...

Linux system disk formatting and manually adding swap partition

Windows: Support NTFS, FAT Linux supports file fo...

Vue realizes the progress bar change effect

This article uses Vue to simply implement the cha...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

Solution to inconsistent display of cursor size in input box

The cursor size in the input box is inconsistent T...

Solve the problem that Docker must use sudo operations

The steps are as follows 1. Create a docker group...

Why not use UTF-8 encoding in MySQL?

MySQL UTF-8 encoding MySQL has supported UTF-8 si...

What are the new CSS :where and :is pseudo-class functions?

What are :is and :where? :is() and :where() are p...

Linux redis-Sentinel configuration details

download Download address: https://redis.io/downl...

CSS eight eye-catching HOVER effect sample code

1. Send effect HTML <div id="send-btn&quo...