Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 will not disable input based on true or false condition

Tags:

angular

I have a input box based on the below:

If a change a 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 this:

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 a reactive from in rc6.

I set the initial disable to the below:

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

        }) 

So do I enable based on a toggle of a radio input?

like image 218
Tampa Avatar asked Sep 05 '16 06:09

Tampa


2 Answers

For boolean properties you need to set them to null to get them removed

<input [disabled]="model_parameters_general.estimationmethod=='ew' ? true : null" [(ngModel)]="model_parameters_general.lambda" 
  formControlName="lambda" type="text" class="form-control">
like image 45
Günter Zöchbauer Avatar answered Sep 30 '22 14:09

Günter Zöchbauer


Try using attr.disabled, instead of disabled

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

StackOverflow Answer

like image 104
Pankaj Avhad Avatar answered Sep 30 '22 14:09

Pankaj Avhad