Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement addClass and removeClass functionality in angular2

Tags:

I have couple of buttons for ListView and GridView. For switching between these 2 buttons I had written JQuery like below-

<script type="text/javascript">     $(document).ready(function () {         $("button.switcher").bind("click", function (e) {             e.preventDefault();             var theid = $(this).attr("id");             var theitems = $("ul#items");             var classNames = $(this).attr('class').split(' ');             if ($(this).hasClass("active")) {                 // if currently clicked button has the active class                 // then we do nothing!                 return false;             } else {                 // otherwise we are clicking on the inactive button                 // and in the process of switching views!                 if (theid == "gridview") {                     $(this).addClass("active");                     $("#listview").removeClass("active");                     // remove the list  view and change to grid                     theitems.removeClass("tilelist");                     theitems.addClass("gridlist");                 }                  else if (theid == "listview") {                     $(this).addClass("active");                     $("#gridview").removeClass("active");                     // remove the grid view and change to list                     theitems.removeClass("gridlist")                     theitems.addClass("tilelist");                 }             }          });     }); </script> 

Now I want move this functionality from Jquery to Angular2 typescript application. Can anyone please guide me on this? How do I implement addClass and removeClass functionality on button click from angular2 template?

like image 829
Gretel Hendricks Avatar asked Aug 04 '16 17:08

Gretel Hendricks


People also ask

What is addClass and removeClass in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

How do I delete a class in angular 10?

$( "p" ). removeClass( "myClass" ). addClass( "yourClass" );

How do I delete a class on Teamspeak?

jQuery removeClass() Method The removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.


2 Answers

If you want to due this in component.ts

HTML:

<button class="class1 class2" (click)="clicked($event)">Click me</button> 

Component:

clicked(event) {   event.target.classList.add('class3'); // To ADD   event.target.classList.remove('class1'); // To Remove   event.target.classList.contains('class2'); // To check   event.target.classList.toggle('class4'); // To toggle } 

For more options, examples and browser compatibility visit this link.

like image 163
Malik Shahzad Avatar answered Oct 19 '22 07:10

Malik Shahzad


Try to use it via [ngClass] property:

<div class="button" [ngClass]="{active: isOn, disabled: isDisabled}"          (click)="toggle(!isOn)">          Click me!      </div>`, 
like image 32
null canvas Avatar answered Oct 19 '22 07:10

null canvas