Skip to content Skip to sidebar Skip to footer

Need To Pass Value From To Parent

Trying to pass a value from child to parent via ng-content Parent Component

Solution 1:

Use EventEmitter with @Output decorator to pass data from child to parent.

ChildComponent:

exportclassChildComponent {
  @Output() str = newEventEmitter<string>();

  pass(str: string) {
    this.str.emit('Pass this string to parent');
  }
}

ParentComponent:

@Component({
  selector: 'app-parent',
  template: `
    <h2>Pass data?</h2>
    <app-child
      (str)="onPassed($event)">
    </app-child>
  `
})
export classParentComponent {
  onPassed(str: string) {
    console.log(str);
  }
}

Fire the event (pass()) when you are looping <div *ngFor="let value form values">.

Post a Comment for "Need To Pass Value From To Parent"