angularangular2-directivesangular9angular-renderer2

More than one class on Renderer.addClass()


How do I add more than just one class on the method Renderer2.addClass();

Example:

this.renderer.addClass(this.el.nativeElement, 'btn btn-primary')

When I try to do so I get the error:

ERROR DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('btn btn-primary') contains HTML space characters, which are not valid in tokens.
    at EmulatedEncapsulationDomRenderer2.addClass

Solution

  • Unfortunately this.renderer.addClass() is accepting only one string without spaces.

    What you can do is using the classList of native element to add multiple classes:

    this.el.nativeElement.classList.add('btn', 'btn-primary');