APIs used Simple ExampleRender subcomponents in a list, click on a subcomponent to notify the parent component to perform an operation person.tsexport interface Person { name: string; age: number; sex: string; } Parent Componentimport { 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); } } Subcomponentsimport { 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 SummarizeThis 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:
|
<<: Pure CSS code to achieve drag effect
>>: A detailed introduction to Linux memory management and addressing
Prepare the database (MySQL). If you already have...
transform: scale(); Scaling will cause jitter in ...
Table of contents Overview Blob Blob in Action Bl...
Recently, a service has an alarm, which has made ...
meta is an auxiliary tag in the head area of htm...
tar backup system sudo tar cvpzf backup.tgz --exc...
Table of contents 1. Task Queue 2. To explain som...
Preface When developing a project, I encountered ...
Table of contents Preface Laying the foundation p...
Preparation 1. Start the virtual machine 2. git t...
1. Space rules Whitespace within HTML code is usu...
Detailed explanation of MySql automatic truncatio...
I remember a question the interviewer asked durin...
First, the server environment information: Reason...
When vue2 converts timestamps, it generally uses ...