Indigosoft / ngxd

✨🦊 NgComponentOutlet + Data-Binding + Full Lifecycle = NgxComponentOutlet for Angular 7, 8, 9, 10, 11, 12, 13, 14, 15, 16+
MIT License
321 stars 29 forks source link

Context error when emitting event from "set" #21

Closed JeoCo closed 4 years ago

JeoCo commented 5 years ago

Hello, I've forked a stackblitz from one of the other (solved) issues here.

I was trying to create a very basic example where the "name" property in the "hello" component is emitting an event every time a "set" occurs (so one could keep track of its value in its parent component).

The following is all I changed: ( explanations in comments)

export class HelloComponent  {
  @Output() myClick: EventEmitter<string> = new EventEmitter<string>();
  @Output() setEvent: EventEmitter<string> = new EventEmitter<string>(); // added a second emitter
  component = DynamicComponent;

  private _name: string; // made name private
  @Input() set name(value : string){
    console.log('set name', value, this); // log so you can see the error and context
    this._name = value; 
    this.setEvent.emit(value); // emitting the event so the parent component (in this case app component) can listen to it and keep track of the value
  }
  get name(){ return this._name;}

}  

I then encounter a very strange phenomenon where "set name()" is run multiple times (without any actual need) with the last of them having an empty context ( this = {} ) thus throwing the following errors:

ERROR TypeError: Cannot read property 'emit' of undefined

ERROR CONTEXT

I should note that I'm specifically interested in the "name" property (that comes via @Input) - the goal is to keep its value synced with the parent in case it changes.

Also I should mention that if you give DynamicComponent a selector and place it directly in your template the issue doesn't occur. I've tested thoroughly seems to be an issue with ngxd.

Could you maybe shed some light on this behaviour? What am I doing wrong? Thanks :)

thekiba commented 5 years ago

Hello! I've seen the bug on a stackblitz link, but I can't repeat it in the tests.

Can you make Pull Request with the failed test? And then I'll fix it asap.

davidhunt135 commented 5 years ago

I have this same problem but I'm not sure on reproducing. It happens whatever I try

alQlagin commented 5 years ago

Hi, @davidhunt135 I've reproduced it and added pr #24 with test

thekiba commented 4 years ago

I published new 9.0.4 version. It should to fix that. @JeoCo @davidhunt135 @alQlagin Thanks 💖