angular2不会根据true或false条件禁用输入

Tam*_*mpa 7 angular

我有一个基于以下输入框:

如果更改收音机,我会看到该值对于false更改为true:

<pre> {{model_parameters_general.estimationmethod=='ew'}} </pre>
Run Code Online (Sandbox Code Playgroud)

那么哇为什么输入框会因为false而被禁用?

<input [disabled]="model_parameters_general.estimationmethod=='ew'" [(ngModel)]="model_parameters_general.lambda" 
                           formControlName="lambda" type="text" class="form-control">
Run Code Online (Sandbox Code Playgroud)

编辑:

在日志中,我得到了这个:

It looks like you're using the disabled attribute with a reactive form directive. If you set disabled to true
      when you set up this control in your component class, the disabled attribute will actually be set in the DOM for
      you. We recommend using this approach to avoid 'changed after checked' errors.

  Example: 
  form = new FormGroup({
    first: new FormControl({value: 'Nancy', disabled: true}, Validators.required),
    last: new FormControl('Drew', Validators.required)
  });
Run Code Online (Sandbox Code Playgroud)

所以我在rc6中使用了反应.

我将初始禁用设置为以下:

this.myForm = fb.group({
                lambda: new FormControl({value: .99, disabled: true}, Validators.required),

        }) 
Run Code Online (Sandbox Code Playgroud)

那么我是否可以根据无线电输入的切换启用?

小智 9

尝试使用 attr.disabled,而不是禁用

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

StackOverflow 答案


Gün*_*uer 6

对于布尔属性,您需要将它们设置为null删除它们

<input [disabled]="model_parameters_general.estimationmethod=='ew' ? true : null" [(ngModel)]="model_parameters_general.lambda" 
  formControlName="lambda" type="text" class="form-control">
Run Code Online (Sandbox Code Playgroud)