Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  24] [ 2]  / answers: 1 / hits: 25472  / 7 Years ago, mon, june 5, 2017, 12:00:00

I'm trying to conditionally apply a transform: translateX($value) directly to the template element using [ngStyle] but I cannot find anyone talking about this. Mainly because I don't know what to search for :D.



I've seen some approaches doing basic styling with [style.color] or something a bit more advanced with [attr.style]={'transform': 'translateX(4rem)'} but the last one returns in the rendered HTML this: style=unsafe. I've seen that you can use the Sanitizer to sanitize or bypass your styles/scripts etc... but at this point it was starting to 'smell' like something that you're not supposed to do? I'm looking for a clean way to achieve this. But I have no clue on where to look.



Thanks in advance to everyone!


More From » css

 Answers
7

You can use [ngStyle] as follows, assuming you have a $value variable in your controller:



[ngStyle]={'transform': 'translateX(' + $value + 'px)'}


and suppose you want to change both the axis having controller variables $x , $y



[ngStyle]={'transform': 'translate(' + $x + 'px, ' + $y + 'px)'}

[#57564] Thursday, June 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
krystadesiraeo

Total Points: 493
Total Questions: 93
Total Answers: 100

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
;