1

I have here a situation where I need to create dynamically components to be shown in a Tab im Angular/PrimeNG.

Tabs are not defined when application is started. Content of the tabs depends on what rights are defined for user and must therefore be created dynamically. Some Tabs may have the same layout, but are filled with different data (Like Tab 1 with booking data from US, Tab 2 with booking data from UK etc, Tab 3 with Payment Data for US, Tab 4 with Payment Data for UK)

So I would like to create everything dynamically. User logs in, I extract his rights and create according to it all Tabs as dynamic component. This works, but I am getting an ugly error in Chrome.

My view looks like:

<p-tableView orientation="left">
  <p-tabPanel *ngFor="let tab of tabs" [header]="tab.title">
    <ng-container *ngComponentOutlet="tab.contentComponent"></ng-container
  </p-tabPanel>
</p-tableView>

Now for the code:

tabs { title: String, contentComponent: any}[] = [];

constructor(..., private viewContainerRef : ViewContainerRef, private injector: EnvironmentInjector) { }

ngOnInit() {
   const bookedRange = this.viewContainerRef.createComponent(BookedRangeComponent, {environmentInjector: this.injector});
    // Setting some Parameter
   bookedRange.instance.filterBookedID = 1000;
   bookedRange.instance.filterUserParam = "AB";
   ...
  this.tabs = [
    { title: "DefaultBookedView", contentComponent: bookedRange }
    ...
  ]

Here the BookedRangeComponent:

@Component({
selector: 'booked-range',
templateUrl: 'booked-range.html',
})
export class BookedRangeComponent {
   filterBookedID : number;
   filterUserParam: string;
....
}

When I start this, this component seems to be created correctly and is also shown in the Tab View with all parameter sets, but in the Development Console of Chrome I see the following Error:

ASSERTION ERROR: It looks like Component Factory was provided as the first argument and an options object as the second argument. This combination of arguments is incompatible. You can either change the first argument to provide component type or change the second argument to be a number (representing an index at which to insert the new component's host view into the container

Same happens, if I remove the option part at the end:

const bookedRange = this.viewContainerRef.createComponent(BookedRangeComponent);

Exactly same error.

Any ideas how to fix it? Or better ideas?

1 Answer 1

1

The error you're getting comes not from the call

this.viewContainerRef.createComponent(BookedRangeComponent)

but from a similar one that *ngComponentOutlet does internally.

*ngComponentOutlet accepts a component class as input and handles everything needed for creating an instance of the component and adding it to the DOM so you don't need to do it by using this.viewContainerRef.createComponent(BookedRangeComponent).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.