Angular2: Parent and Child Components Communication

后端 未结 2 1783
既然无缘
既然无缘 2020-12-21 03:46

I\'m trying to create a parent and child component where the child component is going to have a states drop down. Can someone help me understand how I can access the states

2条回答
  •  心在旅途
    2020-12-21 04:03

    Using output and emit you can easily access child data in parent component. For example

    import { Component,Input,Output,EventEmitter  } from '@angular/core';
    
    @Component({
      selector: 'child-component',
      template: '
    ' +'Child Component: ' +'{{name}}' +'' +'
    ', styleUrls: ['./app.component.css'] }) export class ChildComponent { @Input() name: string; @Output() shareDataToParent = new EventEmitter(); title : string; constructor(){ this.title = 'Sharing from child to parent'; } shareData(){ this.shareDataToParent.emit(this.title); } }

    Parent component

    shareDataToParent(sharedText:string){
      console.log(sharedText);
    }
    

    More info on child parent communication

提交回复
热议问题