Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass events #5

Open
sanyashvets opened this issue Oct 23, 2018 · 12 comments
Open

Cannot pass events #5

sanyashvets opened this issue Oct 23, 2018 · 12 comments
Labels
need doc That issue should be shown in documentation resolved That issue was resolved

Comments

@sanyashvets
Copy link

sanyashvets commented Oct 23, 2018

I have 2 dynamic components with events.
First dymamic component have few events:
Output('action1') action1= new EventEmitter<any>();
Output('action2') action2= new EventEmitter<any>();
Output('action3') action3= new EventEmitter<any>();

Also i have host component for this dynamic components. All components registered in declarations/entry components

HTML of host:
<ng-container *ngxComponentOutlet="component; context: {data: data}">

events in host, like in first dynamic component:
Output('action1') action1= new EventEmitter<any>();
Output('action2') action2= new EventEmitter<any>();
Output('action3') action3= new EventEmitter<any>();

and events doesnt fires on in host wrapper component:

<journal-host [component]="getDisplayedComponent()"
                [data]="data"
                (action1)="onAction1($event)"
                (action3)="onAction2($event)"
                (action3)="onAction3($event)"></journal-host>

Why I cant reach those onAction1/2/3() functions in my wrapper? IDE autocomplete tells me that I have this events, but they doesnt fire. Help please

UPD:
seems like it works when event name isn't specified (Output('action2') action2 = ... => Output() action2 = ....)

@thekiba
Copy link
Contributor

thekiba commented Oct 23, 2018

What version are you using? Can you repeat the problem on stackblitz.com?

Also, you do not need to use HostComponent for your dynamics. This feature is marked as deprecated due to its complexity of use. Instead, use *ngxComponentOutlet on ng-container as a structural directive. The directive will make a bindings on your component automatically.

@ciukstar
Copy link

ciukstar commented Apr 8, 2019

Same issue: "event not caught when emitted by x instance":

<div *ngFor="let x of layouts">
      <ng-container *ngxComponentOutlet="x"
                    (stateChange)="onTemplateChange($event)">
      </ng-container>
</div>

@thekiba
Copy link
Contributor

thekiba commented Apr 8, 2019

@ciukstar you can't do it that way. You need to create an @Output() with EventEmitter and with the same name in host component. All events will be handled by this output.

@thekiba
Copy link
Contributor

thekiba commented Apr 8, 2019

@ciukstar
Copy link

ciukstar commented Apr 8, 2019

Thank you! Now I got it. There must be at least three @Components involved !?

@thekiba thekiba closed this as completed Jun 2, 2019
@thekiba thekiba added need doc That issue should be shown in documentation resolved That issue was resolved labels Jul 17, 2020
@thekiba thekiba reopened this Jul 17, 2020
@balaji-balamurugan
Copy link

how can we emit event from parent to child components

@thekiba
Copy link
Contributor

thekiba commented Sep 23, 2021

@balajibadly, you can use the output of the directive (ngxComponentOutletActivate) to get the component instance and then send any event to the component

@balaji-balamurugan
Copy link

can i have any stackBlitz example @thekiba ?

@balaji-balamurugan
Copy link

can i have any stackBlitz example @thekiba ?

hello @thekiba ?

@balaji-balamurugan
Copy link

no rply ?

@thekiba
Copy link
Contributor

thekiba commented Sep 23, 2021

You have to use (ngxComponentOutletActivate) in the template:

<ng-container 
  [ngxComponentOutlet]="component"
  (ngxComponentOutletActivate)="onActivate($event)">
</ng-container>

And then you'll get a component instance in onActivate callback

  onActivate(component: LazyComponent): void {
    component.something();
  }

@balaji-balamurugan
Copy link

balaji-balamurugan commented Sep 24, 2021

how to trigger this onActivateEvent in a click function!
I need to handle a click function from parent component to projected component , so that the component can do something i want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need doc That issue should be shown in documentation resolved That issue was resolved
Projects
None yet
Development

No branches or pull requests

4 participants