5.5 KiB
ProjectionStrategy
ProjectionStrategy is an abstract class exposed by @abp/ng.core package. There are three projection strategies extending it: ComponentProjectionStrategy, RootComponentProjectionStrategy, and TemplateProjectionStrategy. Implementing the same methods and properties, all of these strategies help you define how your content projection will work.
ComponentProjectionStrategy
ComponentProjectionStrategy is a class that extends ProjectionStrategy. It lets you project a component into a container.
constructor
constructor(
component: T,
private containerStrategy: ContainerStrategy,
private contextStrategy?: ContextStrategy,
)
componentis class of the component you would like to project.containerStrategyis theContainerStrategythat will be used when projecting the component.contextStrategyis theContextStrategythat will be used on the projected component. (default: None)
Please refer to ContainerStrategy and ContextStrategy documentation for their usage.
injectContent
injectContent(injector: Injector): ComponentRef<T>
This method prepares the container, resolves the component, sets its context, and projects it to the container. It returns a ComponentRef instance, which you should keep in order to clear projected components later on.
RootComponentProjectionStrategy
RootComponentProjectionStrategy is a class that extends ProjectionStrategy. It lets you project a component into the document, such as appending it to <body>.
constructor
constructor(
component: T,
private contextStrategy?: ContextStrategy,
private domStrategy?: DomStrategy,
)
componentis class of the component you would like to project.contextStrategyis theContextStrategythat will be used on the projected component. (default: None)domStrategyis theDomStrategythat will be used when inserting component. (default: AppendToBody)
Please refer to ContextStrategy and DomStrategy documentation for their usage.
injectContent
injectContent(injector: Injector): ComponentRef<T>
This method resolves the component, sets its context, and projects it to the document. It returns a ComponentRef instance, which you should keep in order to clear projected components later on.
TemplateProjectionStrategy
TemplateProjectionStrategy is a class that extends ProjectionStrategy. It lets you project a template into a container.
constructor
constructor(
template: T,
private containerStrategy: ContainerStrategy,
private contextStrategy?: ContextStrategy,
)
templateisTemplateRefyou would like to project.containerStrategyis theContainerStrategythat will be used when projecting the component.contextStrategyis theContextStrategythat will be used on the projected component. (default: None)
Please refer to ContainerStrategy and ContextStrategy documentation for their usage.
injectContent
injectContent(): EmbeddedViewRef<T>
This method prepares the container, and projects the template together with the defined context to it. It returns an EmbeddedViewRef, which you should keep in order to clear projected templates later on.
Predefined Projection Strategies
Predefined projection strategies are accessible via PROJECTION_STRATEGY constant.
AppendComponentToBody
PROJECTION_STRATEGY.AppendComponentToBody(
component: T,
contextStrategy?: ComponentContextStrategy<T>,
)
Sets given context to the component and places it at the end of <body> tag in the document.
AppendComponentToContainer
PROJECTION_STRATEGY.AppendComponentToContainer(
component: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
Sets given context to the component and places it at the end of the container.
AppendTemplateToContainer
PROJECTION_STRATEGY.AppendTemplateToContainer(
templateRef: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
Sets given context to the template and places it at the end of the container.
PrependComponentToContainer
PROJECTION_STRATEGY.PrependComponentToContainer(
component: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
Sets given context to the component and places it at the beginning of the container.
PrependTemplateToContainer
PROJECTION_STRATEGY.PrependTemplateToContainer(
templateRef: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
Sets given context to the template and places it at the beginning of the container.
ProjectComponentToContainer
PROJECTION_STRATEGY.ProjectComponentToContainer(
component: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
Clears the container, sets given context to the component, and places it in the cleared the container.
ProjectTemplateToContainer
PROJECTION_STRATEGY.ProjectTemplateToContainer(
templateRef: T,
containerRef: ViewContainerRef,
contextStrategy?: ComponentContextStrategy<T>,
)
Clears the container, sets given context to the template, and places it in the cleared the container.