How can I access a string of text given within the tags of a component
<my-custom-component>THIS TEXT</my-custom-component>
Within a template, I can use ng-content
, or if it is an instance of some other class I can access it within the component definition like demonstrated in these examples. However I am interested in detecting if there is a string of text there or not, which I believe would make providedText
undefined. However, I am always getting undefined.
@ContentChild(Element, { static: true }) providedText: Text | undefined;
I have tried Text
as the first element passed to @ContentChild
. Passing any
will not work (I don't know why).
I am interested mostly in finding if there is a string or undefined, but am also curious why ContentChild(Text...
isn't working.
Edit:
I have added a potential solution, but it seems pretty imperfect, so I hope something better comes along.
Edit 2:
I now understand that @ContentChild is not a mechanism for selecting whatever native HTML I want without wiring it up to Angular’s dependency graph with a ref, directive, etc.
I am still curious if my proposed solution below is a bad idea for any reason.