Angular ngClass I click event dla przełączania klasy

W Angular, chciałbym użyć ngClass i kliknąć event, aby przełączyć klasę. Przeglądałem online, ale niektóre są kątowe1 i nie ma jasnej instrukcji ani przykładu. Każda pomoc będzie mile widziana!

W HTML mam:

<div class="my_class" (click)="clickEvent($event)" ngClass="{'active': toggle}">
  Some content
</div>

W .ts:

clickEvent(event) {
  // Haven't really got far
  var targetEle = event.srcElement.attributes.class;
}
Author: Steve Kim, 2017-06-14

7 answers

To powinno zadziałać.

W .html:

<div class="my_class" (click)="clickEvent()"  
    [ngClass]="status ? 'success' : 'danger'">                
     Some content
</div>

W .ts:

status: boolean = false;
clickEvent(){
    this.status = !this.status;       
}
 119
Author: Mani S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-10-22 22:11:25

Zamiast tworzyć funkcję w pliku ts, możesz przełączyć zmienną z samego szablonu. Następnie można użyć zmiennej do zastosowania określonej klasy do elementu. Like so -

<div (click)="status=!status"  
    [ngClass]="status ? 'success' : 'danger'">                
     Some content
</div>

Więc gdy status jest prawdziwy, stosuje się sukces klasy. Gdy jest fałszywy Klasa niebezpieczeństwa jest stosowana.

To będzie działać bez dodatkowego kodu w pliku ts.

 63
Author: charsi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-30 01:03:40

Angular6 używając renderer2 bez żadnych zmiennych i czystego szablonu:

Szablon:

<div (click)="toggleClass($event,'testClass')"></div>

W ts:

toggleClass(event: any, class: string) {
  const hasClass = event.target.classList.contains(class);

  if(hasClass) {
    this.renderer.removeClass(event.target, class);
  } else {
    this.renderer.addClass(event.target, class);
  }
}

To też można umieścić w dyrektywie;)

 30
Author: neox5,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-08-08 13:45:06

ngClass powinien być owinięty w nawiasy kwadratowe, ponieważ jest to właściwość wiążąca. Spróbuj tego:

<div class="my_class" (click)="clickEvent($event)"  [ngClass]="{'active': toggle}">                
     Some content
</div>

W składniku:

     //define the toogle property
     private toggle : boolean = false;

    //define your method
    clickEvent(event){
       //if you just want to toggle the class; change toggle variable.
       this.toggle = !this.toggle;       
    }
Mam nadzieję, że to pomoże.
 11
Author: Saurabh Tiwari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-03-22 01:37:43

Możemy również użyć ngClass, aby przypisać wiele klas CSS na podstawie wielu warunków, jak poniżej:

<div
  [ngClass]="{
  'class-name': trueCondition,
  'other-class': !trueCondition
}"
></div>
 2
Author: MiPhyo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-06-19 07:17:01

Więc normalnie tworzysz zmienną pomocniczą w klasie i przełączasz ją po kliknięciu i przywiązujesz do zmiennej powiązanie klasy. Coś w stylu:

@Component(
    selector:'foo',
    template:`<a (click)="onClick()"
                         [class.selected]="wasClicked">Link</a>
    `)
export class MyComponent {
    wasClicked = false;

    onClick() {
        this.wasClicked= !this.wasClicked;
    }
}
 1
Author: Hammad Ahmad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-11-18 03:35:17

Jeśli chcesz przełączyć tekst za pomocą przycisku przełączania.

HTMLfile, który używa bootstrap:

<input class="btn" (click)="muteStream()"  type="button"
          [ngClass]="status ? 'btn-success' : 'btn-danger'" 
          [value]="status ? 'unmute' : 'mute'"/>

Plik TS:

muteStream() {
   this.status = !this.status;
}
 0
Author: Bilal Ahmad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-29 11:38:04