angular2 @input and @output understand
Make an example first, and then provide the code
for example:
<talk-cmp [talk]="someExp" (rate)="eventHandler($)">
input, [talk]="someExp" tag can be understood as a special listener that monitors someExp parameters passed by the parent component and stores the talk change of its own component; it seems that a backdoor is opened, allowing only someExp of the parent component to enter. Once entered, it is immediately grabbed into a cell called talk, and then the child component can define this variable talk through @Input and use it.
output , (click)="eventHandler($) This means that when the click event of the child component is triggered, the eventHandler function of the parent component is executed, and the parameter $ of the child component is passed to the eventHandler function of the parent component; it is like when the child cries (executes the click event), his mother immediately holds him in his arms (executes the mother's eventHandler), and the mother obtains some parameters of the child ($)
1、@input()
Parent component provides data
import {Component} from "@angular/core"; @Component({ selector: "my-father", templateUrl: "" }) export class FatherComponent { data: Array<Object>; constructor() { = [ { "id": 1, "name": "html" }, { "id": 2, "name": "css" }, { "id": 3, "name": "angular" }, { "id": 4, "name": "ionic" }, { "id": 5, "name": "node" } ] } }
Template file
<h1>Parent component</h1> // Contains child components and uses properties to pass data past<my-child [info]="data"></my-child>
Subcomponents Get data
import {Component, Input} from "@angular/core"; @Component({ selector: "my-child", templateUrl: "" }) export class ChildComponent { // Use @Input to get the passed data @Input() info: Array<Object>; constructor() { } }
Subcomponents Template File
<ul> <li *ngFor="let item of info"> {{}} </li> </ul>
2、@Output()
Subcomponents
1. Introduce
import {Component, OnInit, Output, EventEmitter} from "@angular/core";
2. Define output variables
export class ThreeLinkComponent { province: string; // Output the parameters @Output() provinceOut = new EventEmitter(); constructor() { = "Shaanxi"; } }
3. Starting from the event, transmitting variables to the parent component
provinceChange() { // When selecting a province, launch the province to the parent component (); }
Parent component template
<!--Three-level linkage components--> <three-link (provinceOut)="recPro($event)"></three-link>
Parent component
// The function accepts variables passed by the subfunction, and this function is triggered when emitted in the subfunction.recPro(event) { = event; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.