1

I have an Ionic app where I am trying to disable the ion-input using reactive forms some thing like this.

Disable(){
  this.name.disable();
  this.name.updateValueAndValidity();
}

And when I try to enable the input element it's not working

 Enable(){
   this.name.enable();
   this.name.updateValueAndValidatity();
 }

The html code looks like this:

  <ion-item>
     <ion-input type="text" [formControl]="name"></ion-input>
  </ion-item>
  <button ion-button (click)="Disable()">Disable</button>
  <button ion-button (click)="Enable()">Enable</button>

The above code works fine with html5 input element but not with Ionic elements. Is there something I am missing? I have made a demo example here: https://stackblitz.com/edit/ionic-nl1hi3?file=pages%2Fhome%2Fhome.html

0

2 Answers 2

1

I have written a directive which subscribes the form control Status Changes and disables the elements using element reference. I don't whether this is correct approach but rather than writing disabled attribute in my html for every element I think this is good.

Because if Ionic team fixes the bug I can just delete the directive without modifying the html code. This is the link where I have the directive implemented: https://stackblitz.com/edit/ionic-bxfjzs?file=directives%2Fdisable-control.directive.ts

I need this directive for ion-input, ion-textarea and ion-radio elements. For other elements Ionic is handling the enable/disable property correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for sharing your solution. It's better indeed.
0

Can you try this ?

<ion-item>
     <ion-input type="text" [formControl]="name" [Disabled]="nameDisabled"></ion-input>
  </ion-item>
  <button ion-button (click)="Disable()">Disable</button>
  <button ion-button (click)="Enable()">Enable</button>

Disable(){
  this.nameDisabled =true;
  this.name.updateValueAndValidity();
}

3 Comments

Thank you. .i did that but Without adding disabled attribute is there any approach to achieve...is this a bug in ionic or is that intended to work in similar way
If you made it work can you please share your experience with us? Post it as an answer it'll be a big help. Thank you
@abhilashreddy I think this is the only way to achieve your goal with ion-input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.