Angular 6-如何根据条件禁用div

时间:2019-06-12 17:13:30

标签: html css angular typescript

我有一个div,它将在单击一个按钮时展开,再次单击div时,它会被单击或会发出警报,但是当我单击Expanded(100%)视图时,它不应被单击,应该就像disable。再次返回上一个(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');
  }
}

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

2 个答案:

答案 0 :(得分:0)

像下面这样写来更改文件:

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

}

您的html文件:

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

答案 1 :(得分:0)

尝试一下:

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

Working Demo