简体   繁体   中英

How disable/enable button with ionic 2?

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.

Now I do a little if in the change function.

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

Have you any idea how to achieve that?

Thanks

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:

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

For changing classes:

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

Check here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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