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

Tutorial on Migrating Projects from MYSQL to MARIADB

Prepare the database (MySQL). If you already have...

Solution to css3 transform transition jitter problem

transform: scale(); Scaling will cause jitter in ...

A brief discussion on the binary family of JS

Table of contents Overview Blob Blob in Action Bl...

A detailed analysis and processing of MySQL alarms

Recently, a service has an alarm, which has made ...

Introduction to the use of http-equiv attribute in meta tag

meta is an auxiliary tag in the head area of ​​htm...

Detailed explanation of docker command to backup linux system

tar backup system sudo tar cvpzf backup.tgz --exc...

JavaScript single thread and asynchronous details

Table of contents 1. Task Queue 2. To explain som...

Thoroughly understand JavaScript prototype and prototype chain

Table of contents Preface Laying the foundation p...

Detailed tutorial on uploading and configuring jdk and tomcat on linux

Preparation 1. Start the virtual machine 2. git t...

How to handle spaces in CSS

1. Space rules Whitespace within HTML code is usu...

Detailed explanation of MySql automatic truncation example

Detailed explanation of MySql automatic truncatio...

What are HTML inline elements and block-level elements and their differences

I remember a question the interviewer asked durin...

Detailed explanation of how to cleanly uninstall Docker

First, the server environment information: Reason...

vue3 timestamp conversion (without using filters)

When vue2 converts timestamps, it generally uses ...