How to pass data from parent to child component in Angular 4

后端 未结 4 1783
一个人的身影
一个人的身影 2021-02-01 04:10

When I try to pass data from parent to child component. I\'m getting undefined message in the console. Data is in the form of array.

parent.component.html

4条回答
  •  太阳男子
    2021-02-01 04:45

    It can be done using Input()decorator. See below code -

    parent.component.ts -

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-parent',
      template: `
        
      `,
      styleUrls: ['./parent.component.css']
    })
    export class ParentComponent{
      parentMessage = "message from parent"
      constructor() { }
    }
    

    child.component.ts -

    import { Component, Input } from '@angular/core';
    
    @Component({
      selector: 'app-child',
      template: `
          Say {{ childMessage}}
      `,
      styleUrls: ['./child.component.css']
    })
    export class ChildComponent {
    
      @Input() childMessage: string;
    
      constructor() { }
    
    }
    

    More Information

提交回复
热议问题