3

I want to use a variable width in my angular 5 component template, just like this:

<style>
    input:checked + .slider:before {
        transform: translateX({{width}}px);
    }
</style>

While 'width' is a variable defined in component!

however the "{{width}}" is not parsed ,how to implement this function.thanks!

5
  • you should be having it as variable and use inline style or apply using a class Commented Jan 17, 2018 at 7:02
  • what could be width value. Can you show us calculation? Commented Jan 17, 2018 at 7:05
  • the width is a number, ex, width:number = 30; Commented Jan 17, 2018 at 7:31
  • Follow this stackoverflow.com/a/35280715/9162349 Commented Jan 17, 2018 at 7:41
  • @Ashraful Islam ,does not work at all.thanks! Commented Jan 17, 2018 at 8:16

1 Answer 1

3

You can use Angular ngStyle in this case:

[ngStyle]="{ width: width }"

Take a look at this demo

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

8 Comments

Since OP asked for applying over CSS over pseudo element, it won't possible inline. Otherwise ngStyle is way to go.
@m98 thank you rapid response! in this case,the style selector is too complex ans not belong to a single element ,could you tell me how to write this in ngstyle. thanks!
@YangRui You're welcome, unfortunately you cannot have pseudoelements within inline styling
@M98,no solution for this?
No, there might be some solution, but since you're going to use pseudo (like :before), the ngStyle does not support it. You want when the input checked, the slider:before use translateX to move. Why don't you try to do it using (let's say) Angular Animation?
|

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.