5

I wanted to compare performance of rendering Angular 2 variable binding [innerText]/{{}} with binding variable as content of pseudo-class(because methods above forces element re rendering)

However, I struggle trying to make angular markup work with css.

That works

CSS:

.my-el:after {
    content: attr(my-attr);
}

HTML

<div class="my-el" my-attr="text"></div>

But after change it to my-attr="{{myVar}}" Angular throws error:

browser_adapter.js:77 EXCEPTION: Template parse errors(...)

So I red that I should use attr.my-attr="{{myVar}}"

But after changing CSS to

.my-el:after {
    content: attr(attr.my-attr);
}

it doesn't work (I guess dot isn't valid symbol here?).

I know that all above may have not much sense, however I'm finding it as interesting problem which I can't solve so far.

Any ideas how to make these two work together? Thanks in advance!

1 Answer 1

9

You will have to bind your value with the following way

<div class="my-el" [attr.my-attr]="myVar"></div>

This way angular will attach the myVar contents to the my-attr attribute

If you need to prepend it with data- use

<div class="my-el" [attr.data-my-attr]="myVar"></div>

Then you can access the value from your css with attr(my-attr) or attr(data-my-attr)

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.