src/placeholder/placeholder.service.ts
Singleton service used to register the container for out-of-flow components to insert into. Also used to insert/remove components from that view.
Properties |
|
Methods |
appendElement | |||||||||
appendElement(element: HTMLElement, id?: any)
|
|||||||||
Defined in src/placeholder/placeholder.service.ts:70
|
|||||||||
Parameters :
Returns :
HTMLElement
|
createComponent | ||||||||||||
createComponent(component: ComponentRef
|
||||||||||||
Defined in src/placeholder/placeholder.service.ts:36
|
||||||||||||
Creates and returns component in the view.
Parameters :
Returns :
ComponentRef<any>
|
destroyComponent | ||||||
destroyComponent(component: ComponentRef
|
||||||
Defined in src/placeholder/placeholder.service.ts:51
|
||||||
Parameters :
Returns :
void
|
hasComponentRef | |||||||||
hasComponentRef(component: ComponentRef
|
|||||||||
Defined in src/placeholder/placeholder.service.ts:55
|
|||||||||
Parameters :
Returns :
boolean
|
hasElement | |||||||||
hasElement(element: HTMLElement, id?: any)
|
|||||||||
Defined in src/placeholder/placeholder.service.ts:84
|
|||||||||
Parameters :
Returns :
boolean
|
hasPlaceholderRef | ||||||
hasPlaceholderRef(id?: any)
|
||||||
Defined in src/placeholder/placeholder.service.ts:63
|
||||||
Parameters :
Returns :
any
|
registerViewContainerRef | |||||||||
registerViewContainerRef(vcRef: ViewContainerRef, id?: any)
|
|||||||||
Defined in src/placeholder/placeholder.service.ts:25
|
|||||||||
Used by
Parameters :
Returns :
void
|
removeElement | |||||||||
removeElement(element: HTMLElement, id?: any)
|
|||||||||
Defined in src/placeholder/placeholder.service.ts:77
|
|||||||||
Parameters :
Returns :
HTMLElement
|
Protected viewContainerMap |
Type : Map<any | ViewContainerRef>
|
Default value : new Map()
|
Defined in src/placeholder/placeholder.service.ts:21
|
Map of id's to secondary |
Protected viewContainerRef |
Type : ViewContainerRef
|
Default value : null
|
Defined in src/placeholder/placeholder.service.ts:17
|
Main |
import {
ComponentRef,
ViewContainerRef,
Injector
} from "@angular/core";
import { Injectable } from "@angular/core";
/**
* Singleton service used to register the container for out-of-flow components to insert into.
* Also used to insert/remove components from that view.
*/
@Injectable()
export class PlaceholderService {
/**
* Main `ViewContainerRef` to insert components into
*/
protected viewContainerRef: ViewContainerRef = null;
/**
* Map of id's to secondary `ViewContainerRef`s
*/
protected viewContainerMap: Map<any, ViewContainerRef> = new Map();
/**
* Used by `Placeholder` to register view-container reference.
*/
registerViewContainerRef(vcRef: ViewContainerRef, id?: any): void {
if (id) {
this.viewContainerMap.set(id, vcRef);
} else {
this.viewContainerRef = vcRef;
}
}
/**
* Creates and returns component in the view.
*/
createComponent(component: ComponentRef<any>, injector: Injector, id?: any): ComponentRef<any> {
if (id) {
if (!this.viewContainerMap.has(id)) {
console.error(`No view container with id ${id} found`);
return;
}
return this.viewContainerMap.get(id).createComponent(component as any, { index: this.viewContainerMap.size, injector });
}
if (!this.viewContainerRef) {
console.error("No view container defined! Likely due to a missing `cds-placeholder`");
return;
}
return this.viewContainerRef.createComponent(component as any, { index: this.viewContainerRef.length, injector });
}
destroyComponent(component: ComponentRef<any>) {
component.destroy();
}
hasComponentRef(component: ComponentRef<any>, id?: any) {
if (id) {
return !(this.viewContainerMap.get(id).indexOf(component.hostView) < 0);
}
return !(this.viewContainerRef.indexOf(component.hostView) < 0);
}
hasPlaceholderRef(id?: any) {
if (id) {
return this.viewContainerMap.has(id);
}
return !!this.viewContainerRef;
}
appendElement(element: HTMLElement, id?: any): HTMLElement {
if (id) {
return this.viewContainerMap.get(id).element.nativeElement.appendChild(element);
}
return this.viewContainerRef.element.nativeElement.appendChild(element);
}
removeElement(element: HTMLElement, id?: any): HTMLElement {
if (id) {
return this.viewContainerMap.get(id).element.nativeElement.removeChild(element);
}
return this.viewContainerRef.element.nativeElement.removeChild(element);
}
hasElement(element: HTMLElement, id?: any): boolean {
if (id) {
return this.viewContainerMap.get(id).element.nativeElement.contains(element);
}
return this.viewContainerRef.element.nativeElement.contains(element);
}
}