Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable input based on a condition

Hello all I have the following piece of code:

<table class="details-table" *ngIf="peop && peopMetadata">
    <tr *ngFor="let attribute of peopMetadata.Attributes">
        <td class="details-property">{{attribute.AttributeLabel}}</td>
        <td [ngSwitch]="attribute.AttributeType">
            <div *ngSwitchCase="'String'">
                <input matInput [(ngModel)] = "peop[attribute.AttributeKey]" />
            </div>
            <div *ngSwitchDefault>{{peop[attribute.AttributeKey]}
            </div>
        </td>
    </tr>
    <div>
        <button ng-click="">Submit</button>
    </div>
</table>

I want to disable the input based on an attribute values say peop[attribute.IsWritable]='false' . How can i achieve this here . Any help is appreciated

like image 420
kauschan Avatar asked Aug 06 '18 20:08

kauschan


People also ask

How do I turn off input in NGIF?

You could use [disabled]="*** your condition ***" instead. Every html attribute on input elemets can be binded by using [attribute] . You could also use the formControlName and set disabled in the controller.

How do I disable input?

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!

How do you enable and disable input In React?

To disable a button when an input is empty with React, we can set the input's value as a state's value. Then we can use the state to conditionally disable the button when the state's value is empty. to create the name state with the useState hook.


2 Answers

INPUT only approach:

<input [disabled]="!peop[attribute.IsWritable]" matInput [(ngModel)] = "peop[attribute.AttributeKey]" />

CONDITIONAL approach:

<ng-container *ngIf="peop[attribute.IsWritable]">
  <input matInput [(ngModel)]="peop[attribute.AttributeKey]" />
</ng-container>

<ng-container *ngIf="!peop[attribute.IsWritable]">
  <span>{{ peop[attribute.AttributeKey] }}</span>
</ng-container>

OR:

<input *ngIf="peop[attribute.IsWritable]" matInput [(ngModel)]="peop[attribute.AttributeKey]" />
<span *ngIf="!peop[attribute.IsWritable]">{{ peop[attribute.AttributeKey] }}</span>
like image 108
danday74 Avatar answered Nov 15 '22 15:11

danday74


I just came across the need to do this and found that I needed to use ng-disabled to do it.

<input id="my-input-id" class="my-input-class" type="number" name="my-input-name" ng-model="MyCtrl.myModel" required ng-disabled="MyCtrl.myProperty && MyCtrl.myProperty !== 'someValue'"></input>

Hope this helps someone else!

like image 32
Gharbad The Weak Avatar answered Nov 15 '22 16:11

Gharbad The Weak