How to enable/disable Autocomplete component of Angular Material based on Angular2?

天大地大妈咪最大 提交于 2019-12-14 03:29:21

问题


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.



来源:https://stackoverflow.com/questions/47091397/how-to-enable-disable-autocomplete-component-of-angular-material-based-on-angula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!