如何启用/禁用基于 Angular2 的 Angular Material 的自动完成组件? [英] How to enable/disable Autocomplete component of Angular Material based on Angular2?

查看:30
本文介绍了如何启用/禁用基于 Angular2 的 Angular Material 的自动完成组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法动态启用/禁用自动完成文本字段组件或除此以外.

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

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

How to do this in Angular Material Autocomplete component?

更新:
SO 上的类似问题 也没有得到解答.我对此做出了回应.

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

推荐答案

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

I got this working by doing this.

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

显然我在这里找到了这个.

这篇关于如何启用/禁用基于 Angular2 的 Angular Material 的自动完成组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆