简体   繁体   English

Angular - 禁用按钮

[英]Angular - Disable Button

<div id="home-container">
    <div class="sidebar">
        <button mat-raised-button [disabled]="true">This is a button</button>
    </div>
    <div class="main-panel">
        <ac-map></ac-map>
    </div>
</div>

Environment:环境:

  • Angular 10 Angular 10
  • Chrome/Firefox (Incognito mode) Chrome/Firefox(隐身模式)

Hi guys,嗨,大家好,

I'm experiencing some undesirable behaviour when displaying my angular project.在显示我的 angular 项目时,我遇到了一些不良行为。 The above example shows a simple component with a button that is disabled by default (I'm using 'true' as a placeholder for a variable).上面的示例显示了一个简单的组件,其中包含一个默认禁用的按钮(我使用 'true' 作为变量的占位符)。 When I load the component the button should be disabled.当我加载组件时,按钮应该被禁用。 HOWEVER.然而。 When the component is loaded the button is enabled for the first second or two and then is disabled - making it look disorganised.加载组件时,按钮在前一秒或两秒内启用,然后被禁用 - 使其看起来杂乱无章。 How can I avoid this?我怎样才能避免这种情况?

Kind regards, Scott.亲切的问候,斯科特。

According to your requirement, I suggest you read the document about the component lifecycle of Angular.根据您的要求,我建议您阅读有关 Angular 组件生命周期的文档。 https://angular.io/guide/lifecycle-hooks https://angular.io/guide/lifecycle-hooks

Please check this out for a live sample.请查看此内容以获取实时样本。

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

/**
 * @title Basic buttons
 */
@Component({
  selector: "button-overview-example",
  templateUrl: "button-overview-example.html",
  styleUrls: ["button-overview-example.css"]
})
export class ButtonOverviewExample implements OnInit {
  disabled = true;
  toggle() {
    this.disabled = !this.disabled;
  }

  ngOnInit(): void {
    this.disabled = false;
  }
}

https://stackblitz.com/edit/toggle-angular-material-button-sample?file=src/app/button-overview-example.html https://stackblitz.com/edit/toggle-angular-material-button-sample?file=src/app/button-overview-example.html

In this sample, I used the variable disabled to indicate the button state, and implemented the OnInit hook which I change false to true .在此示例中,我使用变量disabled来指示按钮 state,并实现了OnInit挂钩,我将其更改为falsetrue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM