1

Lets say I created a custom directive who's selector is [myCustomDirective], this directive has optional input parameter. So it may be used like:

<div myCustomDirective></div>

or

<div [myCustomDirective]="someBooleanFromController"></div>

Meanwhile, inside my directive controller I add pseudo class to elementRef on which directive is applied.

My question is how my css selector for the directive should look like to be applied no matter if I provide optional attribute for directive or not. I tried to use selector like so:

[myCustomDirective]:pseudoClassName { 
   // ... some css
}

It did the trick when myCustomDirective is used without parameter, but it doesn't work in cases when parameter is provided for myCustomDirective.

I also tried selector like this:

[myCustomDirective="true"]:pseudoClassName { 
   // ... some css
}

But it didn't work

1
  • Did you tried my below answer? Commented Jun 28, 2019 at 17:23

1 Answer 1

0

Try the below code:

HTML

app.component.html

<p [appBetterHighlight]="'Yellow'">Highlight with App better directive</p>

CSS

style.css

 p[ng-reflect-app-better-highlight] {
    color: blue !important;
 }

Here's how the html in interpreted in browser tool

<p _ngcontent-c4="" ng-reflect-app-better-highlight="Yellow" style="">Highlight with 
 App better directive</p>

The best way to add a CSS to a particular custom directive it to access it in CSS like below:

p[ng-reflect-{custom directive name}] { 
    // css here
}

stackblitz demo

Sign up to request clarification or add additional context in comments.

1 Comment

@HostBinding decorator would be a better bet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.