Discovering an unofficial method for achieving this task:
constructor(private hostRef: ElementRef) { }
getContentAttr(): string {
const attrs = this.hostRef.nativeElement.attributes
for (let i = 0, l = attrs.length; i < l; i++) {
if (attrs[i].name.startsWith('_nghost-c')) {
return `_ngcontent-c${attrs[i].name.substring(9)}`
}
}
}
ngAfterViewInit() {
// inserting HTML element dynamically
dynamicallyAddedHtmlElement.setAttribute(this.getContentAttr(), '')
}
IMPORTANT: This method allows for styling the newly added HTML element.
The assumption is that the naming convention of this attribute may not remain constant across different Angular versions, potentially causing issues when upgrading. However, updating the solution in such cases would likely be straightforward.
It would be beneficial for Angular to provide a function similar to getContentAttr
that hides some of the internal complexities.