Home > Web Front-end > JS Tutorial > Detailed explanation of component interaction in Angular

Detailed explanation of component interaction in Angular

青灯夜游
Release: 2021-05-07 09:46:43
forward
2406 people have browsed it

This article will give you a detailed introduction to Angular component interaction. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of component interaction in Angular

Angular component interaction

Component interaction: Component communication allows two or more components to share information among.
Usage scenarios: When a certain function is used in multiple components, the specific function can be encapsulated in a sub-component, and specific tasks or workflows can be processed in the sub-component. .
Interaction method:

  • Method 1: Interact through the @Input and @Output decorators.
  • Method 2: Interact through service.

[Related recommendation: "angular tutorial"]


Transfer data from parent component to child component

Through input Type binding passes data from parent component to child component.
The input property is a settable property with the @Input decorator.
Values ​​"flow" into this property when it is bound via property binding.

Some code examples are as follows:

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

@Component({
  selector: 'app-selector',
  template: `
    // 模板代码
  `
})
export class TestComponent {
  @Input() hero: Hero;
  @Input('master') masterName: string;
}
Copy after login

The above example contains two input attributes. The second @Input specifies an alias master for the attribute name masterName of the subcomponent.

Reference the child component in the parent component. Some code examples are as follows:

    <app-hero-child *ngFor="let hero of heroes"
      [hero] = "hero"
      [master] = "master">
    </app-hero-child>
Copy after login

Listen to changes in input attribute values

(1) Use the setter method

Use the setter() method of an input attribute to intercept the value changes in the parent component and take action.

Some code examples are as follows:

export class TestComponent {
	
	@Input()
	set name(name: String) {
		// 逻辑处理
	}
}
Copy after login
(2) Use the ngOnChanges() method

Use the ngOnChanges() method of the OnChanges life cycle hook interface to monitor changes in input attribute values and respond.
Note: When multiple, interactive input properties need to be monitored, this method is more appropriate than using the property's setter method.

Import Input, OnChanges and SimpleChange from @angular/core in the child component

import { Component, Input, OnChanges, SimpleChange } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-version-child&#39;,
  template: `
  // 模板代码
  `
})
export class ChildComponent implements OnChanges {
  @Input() major: number;
  @Input() minor: number;

  ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
    for (let propName in changes) {
      // propName为输入属性的名字
      let changedProp = changes[propName]; // changedProp为SimpleChange对象
      // 其它代码
    }
  }
}
Copy after login

SimpleChange classThe source code is as follows:

/**
 * Represents a basic change from a previous to a new value for a single
 * property on a directive instance. Passed as a value in a
 * {@link SimpleChanges} object to the `ngOnChanges` hook.
 *
 * @see `OnChanges`
 *
 * @publicApi
 */
export declare class SimpleChange {
    previousValue: any;
    currentValue: any;
    firstChange: boolean;
    constructor(previousValue: any, currentValue: any, firstChange: boolean);
    /**
     * Check whether the new value is the first value assigned.
     */
    isFirstChange(): boolean;
}
Copy after login

The parent component listens to the events of the child component

The child component exposes an EventEmitter property. When an event occurs, the child component uses this property to emit (upward ejection) events. The parent component binds to this event property and responds when the event occurs.

The EventEmitter property of a child component is an output property, usually with an @Output decorator.

——Interaction between Angular components


Parent component and child component communicate through service

Parent component and its child component share the same service, using This service enables two-way communication within a component family.

The scope of this service instance is limited to the parent component and its child components. Components outside this component subtree will not be able to access the service or communicate with them.


Detailed documentation

For detailed documentation, please refer to the part related to Angular component interaction

More programming related knowledge , please visit: Introduction to Programming! !

The above is the detailed content of Detailed explanation of component interaction in Angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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