Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - How to disable a div based on condition

I have a div which will expand onclick a button,again when I click on the div, it will be clickable or alert will come,but when I click on expanded(100%) view it should not be clickable,it should be like disable.Again when I returned to previous(25%) view the div should be clickable.Can any one please help me. Here is the code below

app.component.html

<button (click)="change()">change</button>
<div (click)="expand()" [ngClass]="{'old': toggle, 'new': !toggle}"class="old">
  Hello
</div>

app.component.ts

declare var require: any;
import { Component,OnInit, Output, EventEmitter } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
    toggle:boolean = true;
  ngOnInit(){

  }
change(){
    this.toggle = !this.toggle;
  }
expand(){
    alert('hi');
  }
}

style.css

.old{
    width:25%;
    border:1px solid;
    height:200px;
    cursor:pointer;
    background:yellow;
}
.new{
    width:100%;
    border:1px solid;
    height:200px;
    cursor:pointer;
    background:green;
}
like image 297
UIAPPDEVELOPER Avatar asked Nov 17 '25 09:11

UIAPPDEVELOPER


1 Answers

Try this:

<button (click)="change()">change</button>
<div  [class.disabled]="!toggle"  (click)="toggle && expand()" [ngClass]="{'old': toggle, 'new': !toggle}"class="old">
  Hello
</div>

in CSS:

.disabled {
  cursor: not-allowed;
}

Working Demo

like image 65
Ghoul Ahmed Avatar answered Nov 19 '25 23:11

Ghoul Ahmed