简体   繁体   English

Angular 4打字稿,如果布尔值为false则禁用按钮?

[英]Angular 4 typescript to disable a button if a boolean value is false?

I want to write a function in typescript, let's name it 'isActive()', and call it on a button in my html file. 我想用打字稿写一个函数,让我们将其命名为“ isActive()”,然后在我的html文件中的按钮上调用它。 I also have a boolean value called 'isActive' declared. 我还声明了一个名为“ isActive”的布尔值。 Suppose I have 2 buttons, when I click button 1, isActive() is called and if isActive value is set to false, button 2 is disabled. 假设我有2个按钮,当我单击按钮1时,将调用isActive(),如果isActive值设置为false,则会禁用按钮2。 Any help? 有什么帮助吗?

Based on your question, what I understood is: 根据您的问题,我了解的是:

There are total two buttons: A and B. Out of which, if A is clicked then based on value of the updated variable you want to disable button B 共有两个按钮:A和B。如果单击A,则根据更新变量的值,要禁用按钮B

If this the requirement then you can use "click" event from Angular. 如果有此要求,则可以使用Angular中的“ click”事件。 Eg here 例如这里

Your HTML 您的HTML

<button id="a" (click)="fnl($event)">A</button>
<button id="b" [disabled]="!isActive">B</button>

Your TS 您的TS

fnl(e) {this.isActive=!this.isActive}

don't forget to declare isActive as class variable 不要忘记将isActive声明为类变量

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

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