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

Detailed explanation of how to pass values ​​between Angular parent and child components?

青灯夜游
Release: 2021-03-22 18:44:00
forward
2395 people have browsed it

This article will introduce to you the method of passing values ​​between parent and child components in Angular. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

Detailed explanation of how to pass values ​​between Angular parent and child components?

Related recommendations: "angular Tutorial"

<span style="font-size: 20px;">Angular</span> Passing values ​​from parent to child components

Official address: https://angular.cn/guide/component-interaction#component-interaction

1. Parent component passes value to child component

  • # Description: When the parent component passes value to child component, the parent component binds to the selector of the child component. Just the data<app-hero-child></app-hero-child>
  • needs to be introduced when the sub-component receives it inputModuleimport { Component, OnInit, Input} from '@angular/core'
  • The sub-component also needs to use syntax sugar to receive the parameters passed by the parent component @input() transData

1.1 Parent componenthero-parent

1、hero-parent.component.html

<p>这是父组件</p>
<app-hero-child></app-hero-child>
Copy after login

2、hero-parent.component.ts

import { Component, OnInit } from '@angular/core'

@Component({
    selector: 'app-hero-parent',
    templateUrl: './app-hero-parent.component.html',
    styleUrls: ['./app-hero-parent.component.scss']
})
export class HeroesComponent implements OnInit {
    tran_childData:string = '这是父组件传递给子组件的数据'
    constructor() {}
    ngOnInit(): void {}
}
Copy after login
1.2 Child componenthero-child

1、hero-child.component.html

<p>{{transData}}</p>
Copy after login

2、hero-child.component.ts

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

@Component({
    selector: 'app-hero-child',
    templateUrl: './app-hero-child.component.html',
    styleUrls: ['./app-hero-child.component.scss']
})
export class DetailComponent implements OnInit {
    @Input() transData: string
    constructor() {}
    ngOnInit(): void {
        console.log(this.transData)
    }
}
Copy after login
1.3 Rendering

Detailed explanation of how to pass values ​​between Angular parent and child components?

2. The component passes parameters to the parent component

  • Note: When the child component passes parameters to the parent component, it needs to import Output and EventEmitter. Introduce the moduleimport {Component, OnInit, Output, EventEmitter} from '@angular/core'
  • You need to expose a method first when using it@Output() childEvent = new EventEmitter ()Used to use emit to pass data
  • Specific usethis.childEvent.emit('I am the data passed by the child component')

2.1 Child componenthero-child

  1. ##hero-child.component.html<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;button&gt;我是子组件,给父组件传递参数&lt;/button&gt;</pre><div class="contentsignin">Copy after login</div></div>
  2. hero-child.component.ts<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">import { Component, OnInit, Output, EventEmitter} from '@angular/core' @Component({     selector: 'app-hero-child',     templateUrl: './app-hero-child.component.html',     styleUrls: ['./app-hero-child.component.scss'] }) export class DetailComponent implements OnInit {     @Output() childEvent = new EventEmitter()     constructor() {}     ngOnInit(): void {},     // 给父组件传递参数     transData_to_parent() {         this.childEvent.emit('我是子组件传递的数据')     } }</pre><div class="contentsignin">Copy after login</div></div>

2.2 Parent componenthero-parent

1. hero-parent.component.html

<p>这是父组件</p>
<p>{{receiceData}}</p>
<app-hero-child></app-hero-child>
Copy after login
2. hero-parent.component.ts

import { Component, OnInit } from '@angular/core'
	
@Component({
    selector: 'app-hero-parent',
    templateUrl: './app-hero-parent.component.html',
    styleUrls: ['./app-hero-parent.component.scss']
})
export class HeroesComponent implements OnInit {
    constructor() {}
    ngOnInit(): void {}
    receiceData:string
    // 接收子组件传递的数据
    receive_child_data(data) {
        this.receiceData = data
    }
}
Copy after login

2.3 Rendering

Detailed explanation of how to pass values ​​between Angular parent and child components?

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of Detailed explanation of how to pass values ​​between Angular parent and child 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!