Home > Web Front-end > JS Tutorial > body text

How to communicate between Angular parent and child components? A brief analysis of the method of passing values ​​from father to son

青灯夜游
Release: 2021-08-10 10:02:07
forward
1828 people have browsed it

AngularHow to communicate between parent and child components? This article will introduce you to the value transfer method of Angular parent-child components.

How to communicate between Angular parent and child components? A brief analysis of the method of passing values ​​from father to son

Passing values ​​through Input and Ouput

Parent component: html and ts

<app-liftcycle [name]="name" (changeName)="changeName($event)"></app-liftcycle>
Copy after login
public name: string = "jack";
public changeName(value: string) {
    this.name = value;
}
Copy after login

Child component: html and ts

<div (click)="emit()">{{name}}</div>
Copy after login
import { Component, Input, EventEmitter, Output } from &#39;@angular/core&#39;;
@Input() name: string;
@Output() changeName: EventEmitter<string> = new EventEmitter<string>();
public emit() {
    this.changeName.emit("修改name属性");
}
Copy after login

[Related tutorial recommendation: "angular tutorial"]

Listen to changes in properties through setters

Parent Components are the same as above, sub-components:

private _name: string = "";
@Input() 
public get name(): string {
    return this._name;
}
public set name(value: string) {
    this._name = value + "定义结构";
}
Copy after login

Monitor changes in input attributes through the ngOnChanges hook function

ngOnChanges is simpler than the setter method when monitoring multiple attributes. .

@Input() name: string;
ngOnChanges(changes: SimpleChanges): void {
    (({name}) => {
        console.log(name.currentValue,name.previousValue);
    })(changes);
}
Copy after login

The methods and properties of the child component are called through template variables in the parent component html.

The template variable obtains a reference to the child component. Parent component:

<app-liftcycle #child></app-liftcycle>
<button (click)="child.childFn()">按钮</button>
Copy after login

Child component:

public childFn() {
    console.log("通过模板变量调用子组件中的方法");
}
Copy after login

The parent component obtains the child component instance through ViewChild

<app-liftcycle [name]="name" (changeName)="changeName($event)" #child></app-liftcycle>
<button (click)="childFn()">childFn</button>
Copy after login
@ViewChild("child") child: LiftcycleComponent;
public childFn(): void {
    this.child.childFn();
}
Copy after login

Communicates through service

service:

import { Subject } from &#39;rxjs&#39;;
import { Injectable } from &#39;@angular/core&#39;;

@Injectable({
    providedIn: &#39;root&#39;
})
export class CommunService {

    constructor() {}
    public commun = new Subject<string>();
    communSend() {
        this.commun.next("send");
    }
}
Copy after login

Parent component:

constructor(private commun: CommunService) { }
public send(): void {
    this.commun.communSend();
}
Copy after login

Child component:

constructor(private commun: CommunService) { 
    this.commun.commun.subscribe((value) => {console.log(value)});
}
Copy after login

Parent component delivery method

The parent component passes the attribute to the child component method, and the child component calls it. This is generally not recommended. React uses this communication method. It may be that the binding based on this is complicated, so angular is not recommended. The emergence of React Hooks is also partly due to This is the intricacy of the class class. Parent component:

<app-liftcycle [send]="send.bind(this)"></app-liftcycle>
Copy after login
public name: string = "jack";
public send(): void {
    console.log(this.name);
}
Copy after login

Child component:

<button (click)="childSend()">childSend</button>
Copy after login
@Input() send: Function;
public childSend() {
    this.send();
}
Copy after login

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to communicate between Angular parent and child components? A brief analysis of the method of passing values ​​from father to son. For more information, please follow other related articles on the PHP Chinese website!

source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!