1

I'm trying to set a custom attribute in a div so that my css selector can find it. But I can't find the right syntax.

css:

 div[customAttr="dabadeedabadie"] {
    color:blue
 }

html:

<div customAttr="dabadeedabadie">I'm blue</div>

That works, but if I try with angular:

<div customAttr="{{'dabadeedabadie'}}">I'm blue</div>

I get an error: Can't bind to 'customAttr' since it isn't a known property of 'div'. Well... right but why is that a problem ?

I also tried :

<div attr.customAttr="'dabadeedabadie'">I'm blue</div>
<div attr.customAttr="{{'dabadeedabadie'}}">I'm blue</div>
<div [attr.customAttr]="'dabadeedabadie'">I'm blue</div>
<div [attr.data-customAttr]="'dabadeedabadie'">I'm blue</div>

Those don't throw an error but don't work either.

How can I set a custom tag property recognized by the css selector with angular ?

0

1 Answer 1

2

It works both of ways

using parameter in ts and with hard coded

<div [attr.customAttr]="'dabadeedabadie'">I'm blue</div>
<div [attr.customAttr]="name">I'm blue with param</div>

TS

 name = 'dabadeedabadie';

CSS

 div[customAttr="dabadeedabadie"] {
    color:blue
 }
Sign up to request clarification or add additional context in comments.

Comments

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.