I'd like to create a structural directive which behaves as follows:
<p *myDirective="condition">This is some text</p>
- If
condition
isfalse
then the<p>
tag is not rendered at all. - If
condition
istrue
then the<p>
tag is rendered with an extraclass
attribute.
So, either there's nothing rendered, or:
<p class="my-added-class">This is some text</p>
In other words, it's a bit like *ngIf
, but with additional behaviour.
I can find examples of how to do the include/exclude behaviour (in fact there is such an example in the Angular docs). I can also find examples of how to add a class to an element using the Renderer2
API.
However, I don't see how I can combine these techniques, because the first method manipulates the viewContainer
to create an embedded view, whereas the second method uses the renderer to manipulate an element.
Is there a way to do this? Can I somehow create the embedded view and then manipulate the elements it creates? Or can I manipulate the template to change how the view is rendered?
[NOTE: @HostBinding
does not work with structural directives, so that's not an option]
This is some text
` – SiddAjmera Nov 15 '18 at 18:14