Reputation: 319
Just try to clarify the meaning of an index parameter in ViewContainerRef.createComponent
:
createComponent<C>(
componentFactory: ComponentFactory<C>,
index?: number,
injector?: Injector,
projectableNodes?: any[][],
ngModule?: NgModuleRef<any>
): ComponentRef<C>
Lets review the index parameter: I've created small example https://plnkr.co/edit/sbDomj And I set up the index as 0. It's OK. THe example works. But If you change this value (1) then the component isn't being added. Why? What does this parameter response of?
Upvotes: 4
Views: 2019
Reputation: 657731
You can add multiple components to a ViewContainerRef
s hostView.
The default (no index specified) means, the new component is added at the end of the list.
If an index is specified, then the new component is inserted at that position. If an invalid position is added (1 is invalid for an empty list), then you get the described behavior. If the list would already contain 2 components, then 1 would be a valid index and your call would insert the component between the first and the 2nd.
See also https://angular.io/api/core/ViewContainerRef#createComponent
Upvotes: 5