How to enable/disable Autocomplete component of An

2019-10-04 07:49发布

问题:

I'm not able to enable/disable the autocomplete textfield component dynamically or otherwise.

I want the component to be disabled by default. And later on some user activity, like on click of a button this should enable the component.

I tried the following code but didnt work:

export class MyComponent {
  // this DIDN'T WORK
  opts:object={value: "", disabled: true};
  ctrl= new FormControl(this.opts);

  ngDoCheck(){  
    // this DIDN'T WORK
    // this could be ngOnChange, ngOnInit or button click event
    if(this.IsTextBoxEnabled){
      this.ctrl.enable();
    }else{
      this.ctrl.disable();
    }
  }
}

How to do this in Angular Material Autocomplete component?

UPDATE:
Similar issue on SO was also left unanswered. To which I responded with my answer.

回答1:

I got this working by doing this.

<input [attr.disabled]="IsTextBoxDisabled?'true':null">

Apparently I found this here.