在Angular中,可以使用按钮的disabled
属性来禁用按钮,并使用*ngIf
指令根据条件来显示或隐藏元素。
下面是一个示例,演示了如何在同一条件下禁用按钮并使用ngIf
显示或隐藏元素:
按钮已禁用
// 组件
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {
isDisabled: boolean = true;
}
在上面的示例中,使用了一个名为isDisabled
的布尔变量来控制按钮的禁用状态和元素的显示或隐藏。当isDisabled
为true
时,按钮将被禁用,并且元素将显示。当isDisabled
为false
时,按钮将可用,并且元素将隐藏。
你可以根据需要在组件中更新isDisabled
变量的值,以控制按钮和元素的状态。
上一篇:按钮禁用/启用时的可访问性问题