Javascript 如何使用 ionic 2 禁用/启用按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42025281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:50:33  来源:igfitidea点击:

How disable/enable button with ionic 2?

javascriptangularionic-frameworkionic2

提问by anubis

I have an input field and a button. It must be disable at start. When the input no blank is, the button is enable.

我有一个输入字段和一个按钮。它必须在启动时禁用。当输入无空白时,按钮启用。

I use a ngModel to take the value of the input and a function (change) to start a function each time the input is changed.

我使用 ngModel 来获取输入的值,并在每次更改输入时使用一个函数(更改)来启动一个函数。

Now I do a little if in the change function.

现在我在 change 函数中做了一点。

if(input !== ''){
//enable the button
}else{
//disable the button
}

Have you any idea how to achieve that?

你知道如何实现这一目标吗?

Thanks

谢谢

回答by Suraj Rao

Just have a boolean variable in class:

只需在类中有一个布尔变量:

isenabled:boolean=false;

Change function

更改功能

if(input !== ''){
//enable the button
isenabled=true; 
}else{
//disable the button
isenabled=false;
}

In Html:

在 HTML 中:

<button ion-button [disabled]="!isenabled"></button>

For changing classes:

换班:

<button ion-button [ngClass]="{class:isenabled,class2:!isenabled}"></button>

Check here

在这里查看