15

I currently have some logic that I would like to simplify if possible using only the html template on (click)

I have a collapsible div that when clicked, becomes "active"

currently my div is:

<div class="collapsible-header blue darken-2" (click)="getDataForTable($event)">

I am then checking for the list of classes on the element

function getDataForTable($event: any){
  let classList = $event.target.classList;

  if(classList.contains("active")){
  //do nothing div is closing
  } else {
  //get data for table since we are opening the div to show the body
  }
}

I want this (click) action only to fire if the class is not "active" (meaning don't fire when clicked as "active");

how can I do this with template syntax only?

2
  • So what is the problem? Did you try to add class active in else-branch? Commented May 22, 2016 at 19:03
  • it works, but I want to know if I can have the logic on the template only so that the (click) event will only fire if its not currently "active" rather than the function firing every click and me checking the event to see if it is active or not Commented May 22, 2016 at 19:05

2 Answers 2

18

You should be able to do it like this:

<div class="collapsible-header blue darken-2" 
     (click)="$event.target.classList.contains('active') || getDataForTable($event)">

And then in the function you would just need to add class:

function getDataForTable($event: any) {
  $event.target.classList.add('active');
  // get data for table since we are opening the div to show the body
}
Sign up to request clarification or add additional context in comments.

Comments

16
<div #mydiv 
    class="collapsible-header blue darken-2"    
    (click)="mydiv.classList.contains('xxx') ? getDataForTable($event) : null">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.