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

A brief discussion on the interaction methods of Angular components

青灯夜游
Release: 2021-04-01 10:22:18
forward
2185 people have browsed it

This article will talk with you about the interaction methods of Angular components. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on the interaction methods of Angular components

Angular component interaction

Component interaction: Component communication allows two Share information between one or more components.

Usage scenarios: When a certain function is used in multiple components, the specific function can be encapsulated in a sub-component and specific tasks can be processed in the sub-component. or workflow.

Interaction method:

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

Recommended related tutorials: "angular tutorial"


##Transfer data from parent component to child component

Transmit data from parent component to child component through input binding.

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 property 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 Listens for changes in input property values ​​and responds.


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 services

Parent component and its child component Share the same service and use this service to implement two-way communication within the 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.


For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of A brief discussion on the interaction methods of Angular components. 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