Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular [disabled]="MyBoolean" not working

I have some inputs (Checkboxes) and I want them to be disabled if my Booleans are true. But its not working... The funny thing is the submit button works just fine and thats the same method...

myComponent.html

          <form [formGroup]="BetreuungsoptionForm" (ngSubmit)="onSubmit()">             <label *ngIf="!eingetragen" for="art">Art</label>             <select *ngIf="!eingetragen" formControlName="art" id="art" class="form-control" [(ngModel)]="Art" required >               <option value="festeAnmeldung">feste Anmeldung</option>               <option value="flexibleAnmeldung">flexible Anmeldung</option>             </select>             <label for="datum">Beginn Datum</label>             <input formControlName="datum" type="date" id="datum" class="form-control" required>             <label *ngIf="(Art == 'festeAnmeldung')" for="montag">Montag</label>             <input *ngIf="(Art == 'festeAnmeldung')" formControlName="montag" [disabled]="montag" type="checkbox" id="montag" class="form-control wochentag">             <label *ngIf="(Art == 'festeAnmeldung')" for="dienstag">Dienstag</label>             <input *ngIf="(Art == 'festeAnmeldung')" formControlName="dienstag" [disabled]="dienstag" type="checkbox" id="dienstag" class="form-control wochentag">             <label *ngIf="(Art == 'festeAnmeldung')" for="mittwoch">Mittwoch</label>             <input *ngIf="(Art == 'festeAnmeldung')" formControlName="mittwoch" [disabled]="mittwoch" type="checkbox" id="mittwoch" class="form-control wochentag">             <label *ngIf="(Art == 'festeAnmeldung')" for="donnerstag">Donnerstag</label>             <input *ngIf="(Art == 'festeAnmeldung')" formControlName="donnerstag" [disabled]="donnerstag" type="checkbox" id="donnerstag" class="form-control wochentag">             <label *ngIf="(Art == 'festeAnmeldung')" for="freitag">Freitag</label>             <input *ngIf="(Art == 'festeAnmeldung' )" formControlName="freitag" [disabled]="freitag" type="checkbox" id="freitag" class="form-control wochentag">         <button type="submit" [disabled]="!BetreuungsoptionForm.valid" class ="btn btn-primary">Speichern</button>         <button type="button" (click)="OnBetreuungsoptionInfos()" class ="btn btn-success">weitere Informationen</button>         <button type="button" *ngIf="!gekuendigt" (click)="OnBetreuungsoptionLoeschen()" class ="btn btn-danger">Kündigen</button>       </form> 

myComponent.ts

        this.BetreuungsoptionForm = new FormGroup         ({           art: new FormControl(),           datum: new FormControl(this.BetreuungsoptionenKindRef[d].Beginn.toString().substring(0,10)),           montag: new FormControl(this.BetreuungsoptionenKindRef[d].Montag),           dienstag: new FormControl(this.BetreuungsoptionenKindRef[d].Dienstag),           mittwoch: new FormControl(this.BetreuungsoptionenKindRef[d].Mittwoch),           donnerstag: new FormControl(this.BetreuungsoptionenKindRef[d].Donnerstag),           freitag: new FormControl(this.BetreuungsoptionenKindRef[d].Freitag)         })           if(this.BetreuungsoptionenKindRef.Montag)           {             this.montag = true;           }           if(this.BetreuungsoptionenKindRef.Dienstag)           {             this.dienstag = true;           }           if(this.BetreuungsoptionenKindRef.Mittwoch)           {             this.mittwoch = true;           }           if(this.BetreuungsoptionenKindRef.Donnerstag)           {             this.donnerstag = true;           }           if(this.BetreuungsoptionenKindRef.Freitag)           {             this.freitag = true;           } 
like image 676
Kajot Avatar asked May 02 '18 09:05

Kajot


People also ask

How does disabled work in angular?

The ng-disabled directive sets the disabled attribute of a form field (input, select, or textarea). The form field will be disabled if the expression inside the ng-disabled attribute returns true. The ng-disabled directive is necessary to be able to shift the value between true and false .

How do you make a field Disabled in HTML?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!

Can't bind to disabled since it isn't a known property of A?

Another way to understand this that disabled property is actually an @Input property that is defined by form related directives, like FormControl or FormControlName and ... , so if you don't use those directives, you can't have disabled attributes.


1 Answers

Try [attr.disabled]="freitag? true : null" or [attr.readonly]="freitag" instead.

You are able to use attributes like [class.btn-lg]="someValue" in a similar way.

Your textbox works because of this:

The disabled attribute is another peculiar example. A button's disabled property is false by default so the button is enabled. When you add the disabled attribute, its presence alone initializes the button's disabled property to true so the button is disabled.

Adding and removing the disabled attribute disables and enables the button. The value of the attribute is irrelevant, which is why you cannot enable a button by writing <button disabled="false">Still Disabled</button>.

from https://angular.io/guide/template-syntax

like image 170
reckface Avatar answered Oct 06 '22 01:10

reckface