Angular2 will not disable input based on true or false state

I have an input box based on below:

If I change the radio, I see that the value changes to true for false:

<pre> {{model_parameters_general.estimationmethod=='ew'}} </pre>

So wow, why will the input box be disabled based on true for false?

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

EDIT:

In the logs, I get the following:

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

So, I am using reactive from rc6.

I set the initial disable below:

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

        }) 

So what can I enable based on switching radio input?

+4
source share
4 answers

For boolean properties you need to set them to nullto remove them

<input [disabled]="model_parameters_general.estimationmethod=='ew' ? true : null" [(ngModel)]="model_parameters_general.lambda" 
  formControlName="lambda" type="text" class="form-control">
+6
source

Try using attr.disabled instead of disabled

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

fooobar.com/questions/1653645 / ...

+4
source

It seems that in rc.6 they removed the ability to dynamically disable input when binding a template using reactive forms. You must track the changes and call .disable()on formControlwhich you want to disconnect now. I prefer the old way of doing something and hoping that they will return it or a useful solution.

Inclusion of this github related issue .

+3
source

Try the following code.

setControl(name: any, Isenable: boolean): void {
    let ctrl = this.checkInForm.controls[name];
    console.log(ctrl);
    Isenable ? ctrl.enable() : ctrl.disable();
}

Defiant expression

this.setControl('ctrl name', true);    // enbale it
  this.setControl('ctrl name', false);    // disable it

Thank you. Happy coding!

+3
source

Source: https://habr.com/ru/post/1653644/


All Articles