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

How to use ngx-component-outlet in ng-template #2

Open
st-clair-clarke opened this issue Jul 15, 2018 · 4 comments
Open

How to use ngx-component-outlet in ng-template #2

st-clair-clarke opened this issue Jul 15, 2018 · 4 comments
Labels
need doc That issue should be shown in documentation resolved That issue was resolved

Comments

@st-clair-clarke
Copy link

Hi,
I like ngx-component-outlet. However, I would like to use in ng-template, similar to the example below with another dynamic component creation library:

     <ng-template matExpansionPanelContent>
          <ndc-dynamic
              [ndcDynamicComponent] = 'component'
              [ndcDynamicInputs] = 'componentInputs'
              [ndcDynamicOutputs] = 'componentOutputs'></ndc-dynamic>
        </ng-template>

Can I do something similar with ngx-component-outlet?
Cheers

@thekiba
Copy link
Contributor

thekiba commented Jul 15, 2018

Hello! Of course, you can use any dynamic component as regular and everything will work.

@st-clair-clarke
Copy link
Author

Thanks for the quick reply.

Using the example above, how would I use your outlet with the ng-template with the component, componentInputs and componentOutputs above.

I have been trying it for sometime without any success.

Cheers

@thekiba
Copy link
Contributor

thekiba commented Jul 15, 2018

You would like use componentInputs and componentOutputs instead of regular binding?

@thekiba
Copy link
Contributor

thekiba commented Jul 15, 2018

An example how to use ngx-component-outlet in ng-template:
https://stackblitz.com/edit/angular-ngx-component-outlet-with-ng-template

The host component on which the rendering and binding are hanging

@Component({
  selector: 'app-dynamic-host',
  template: ''
})
export class DynamicHostComponent {
  @Input() regularInput;
  @Output() regularOutput = new EventEmitter<any>();
}

Dynamic component you would like use as regular

@Component({
  selector: 'app-dynamic',
  template: `
  <button (click)="regularOutput.emit($event)">
    Hello, {{ regularInput }}
  </button>`
})
export class DynamicComponent {
  @Input() regularInput;
  @Output() regularOutput = new EventEmitter<any>();
}

AppComponent

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 6';
  component = DynamicComponent;
  onClick($event) {
    alert($event);
  }
}

And your template

<ng-template #template>
  <app-dynamic-host
    [ngxComponentOutlet]="component"
    [regularInput]="name"
    (regularOutput)="onClick($event)"
  ></app-dynamic-host>
</ng-template>

<ng-container *ngTemplateOutlet="template"></ng-container>

@thekiba thekiba closed this as completed Jul 20, 2018
@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
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

2 participants