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

查看:148
本文介绍了如何基于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天全站免登陆