Search code examples
htmlangulartypescriptng-classangular-ng-if

Angular - Activating a class when another class is activated by JS


I want to activate a class xyz when class xy is activated using ngClass.
I want to implement this scenario:

if class xyz is in the tag
activate class xy
ngClass="'xyz' then xy"

Solution

  • In Primeng-datatable checkboxed selection, I want to show a customized tick mark. So when checkbox is toggled, I want to activate and deactivate a specific class when checkboxes are toggled.

    In Order to achieve that you can do this in html

    <p-checkbox (onChange)="onChange($event)" inputId="checkBoxId" name="groupname" value="val1" [(ngModel)]="selectedValues"></p-checkbox>
    

    in ts

        onChange(checked:boolean){
         let el=document.getElementById('checkBoxId');
    if(checked){
          if(el)
          {
           el.classList.Add('xyz');
          }
         }
    else{
       if(el)
          {
           el.classList.Remove('xyz');
          }
    }
        }