angular

Angular2 renderer setElementClass isAdd options does not work


Why doesn't this work, as stated in the docs ?

renderer.setElementClass(el, 'class1', false); // replace class
renderer.setElementClass(el, 'class2', true); // add a class

This results in the element only have the class2 instead of both.

Reference Angular2 renderer docs


Solution

  • It turns out the isAdd option is the equivalent of a remove class, so the following works for toggling classes:

    renderer.setElementClass(el, 'class1', false); // remove class1
    renderer.setElementClass(el, 'class2', true); // add class2
    

    Oh, nothing strange about calling a method setElementClass for removing it of course...