I'm trying to create a custom directive to replace the inner text of my custom directive. I can't seem to access the inner text content to apply some logic.
Here's the code:
import { Directive, ElementRef, Renderer2, ViewContainerRef } from '@angular/core';
@Directive({
selector: 'text-transformer'
})
export class TextTransformerDirective {
constructor(
private elem: ElementRef) {
// need access to inner text before setting new text
// elem.nativeElement.outerHTML, innerHTML, outerText, innerText are all empty at this point
elem.nativeElement.outerHTML = '<span>My new Text</span>';
}
}
Usage:
<text-transformer>Some text</text-transformer>
I would like to inspect the text inside the tag, in this case, "Some text". I can't seem to access it inside the directive.
Should I use component instead?