-2

I have followed the approach mentioned at the the below link Pass data from child to parent component Angular2

but when i try to assign the value a variable in parent after emitting the event from child it throws and error saying ""Cannot set property of undefined" The error is at second line in the below code

public doSomething(date: any):void {
    this.ParentDate=date;
}
  • 1
    Post a complete minimal example reproducing the issue, in a plunkr or in stackblitz, along with the exact and complete error you get. There's no way for the code you posted to throw that error. – JB Nizet Jan 01 '18 at 16:15
  • I have changed my second line of the code. When the control comes back to parent component after the emit, it is not able to access the parent component. It says "Cannot set property 'ParentDate' of undefined". – V Pradeep Kumar Jan 02 '18 at 06:14

1 Answers1

0

Please check that whether you passed $event in doSomething() method in html side.

Anand Sinha
  • 322
  • 3
  • 7
  • Parent Component: Html:
    Component.ts: public show: boolean; @ViewChild('parent', { read: ViewContainerRef }) container: ViewContainerRef; const comp = this._cfr.resolveComponentFactory(ChildComponent); const childComponent = this.container.createComponent(comp); childComponent.instance.notify.subscribe(this.onNotify);
    – V Pradeep Kumar Jan 02 '18 at 06:25
  • Child Component: Html: Component: @Output() notify: EventEmitter = new EventEmitter(); saveCondition() { this.notify.emit(true); } The error in the parent component: public onNotify(showChild: boolean): void { this.show = showChild; } The error is in the second line at 'this' "Cannot set property 'show' of undefined". – V Pradeep Kumar Jan 02 '18 at 06:28
  • you declare show property in the parent. I try to access in child component. It can not be achieved directly. You have to use @Input. – Anand Sinha Jan 02 '18 at 09:52
  • I am not accessing show property in the child. I am emitting the event from child and in the parent i am trying to assign the show property, the value emitted from child. public onNotify(showChild: boolean): void { this.show = showChild; } The above code is in the parent not in child. – V Pradeep Kumar Jan 02 '18 at 10:25