UIAPPDEVELOPER
UIAPPDEVELOPER

Reputation: 649

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;
}

Upvotes: 3

Views: 20329

Answers (2)

Ghoul Ahmed
Ghoul Ahmed

Reputation: 4836

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

Upvotes: 5

srjkaa
srjkaa

Reputation: 336

Change your files like a write below:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'my-app',
  styleUrls: ['./app.component.scss'],
  templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {

  public toggle: boolean = true;

  ngOnInit(): void {

  }

  public change(): void {
    this.toggle = !this.toggle;
  }

  public expand(): void {
    if (this.toggle) {
      alert('hi');
    }
  }

}

And your html file:

<button (click)="change()">change</button>
<div (click)="expand()" [class.new]="!toggle" class="old">
    Hello
</div>

Upvotes: 2

Related Questions