首页 > 解决方案 > Angular 6 - 如何根据条件禁用 div

问题描述

我有一个 div 会在单击按钮时展开,当我再次单击该 div 时,它将是可单击的或会出现警报,但是当我单击展开的(100%)视图时它不应该是可单击的,它应该像禁用.再次当我回到以前的(25%)视图时,div应该是可点击的。任何人都可以帮助我。这是下面的代码

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

样式.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;
}

标签: htmlcssangulartypescript

解决方案


尝试这个:

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

在 CSS 中:

.disabled {
  cursor: not-allowed;
}

工作演示


推荐阅读