Tuesday 27 December 2016

Polymer add class when property is true




I have seen this in angular before and wondered if this is possible in polymer as well. Angular - What is the best way to conditionally apply a class?



I have set up a property named 'animated':



animated: {
type: Boolean,
value: false,
},



When animated is true, a div inside my element should have a css class of .animate.






For now I have done that inside of the ready function.
But since I came across that Stackoverflow question I wondered if this is prossible in polymer.



Thanks!



Answer



One way to do that is using a function as follow:






Where class$ with $ symbol indicates to Polymer's that property is generate using data binding. So, your _getClass function will look like this:



_getClass: function(animated){
return animated ? "animate" : "";

}


When animate property changes, the _getClass function will be invoked and this function will return the string that indicates the class you need.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...