如何根据Angular2启用/禁用角度材质的自动完成组件?

时间:2017-11-03 08:31:42

标签: angular angular-material2

我无法动态启用/禁用autocomplete textfield component或其他方式。

我希望默认情况下禁用该组件。稍后在一些用户活动上,比如点击按钮,这应该启用该组件。

我尝试了以下代码,但没有工作:

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();
    }
  }
}

如何在Angular Material Autocomplete组件中执行此操作?

更新
Similar issue on SO也未得到答复。我回答了我的回答。

1 个答案:

答案 0 :(得分:0)

我通过这样做得到了这个。

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

显然我发现了here