1

is there a way to disable an option of a select element in angular 4?

I tried something like this but it does not work

<select type="number" [(ngModel)]="selectedItem">
    <option [disabled]="i.disabled" *ngFor="let i of items" [ngValue]="i.id">{{i.designator}}</option>
</select>

Whereabout i.disabled contains a boolean.

Many thanks in advance

4
  • [disabled]="true" Commented Jun 18, 2017 at 19:39
  • I just added [disabled]="true" but still all options are not disabled Commented Jun 18, 2017 at 19:43
  • your code is working without any changes. What is your array object looks like? Commented Jun 18, 2017 at 19:46
  • Thank you! You were right. I was doing the changes on a different component. Many thanks. it was my fault Commented Jun 18, 2017 at 19:55

2 Answers 2

6

It works fine here

<select type="number" [(ngModel)]="selectedItem">
    <option [disabled]="i.disabled" *ngFor="let i of items" [ngValue]="i.name">{{i.name}}</option>
</select>

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

Ok nice, but what about dynamic disabling, based on some other action, from some method triggered in component for example. Is that possible ?
1
this.items = [{id:1,disabled:true,designator:123}];

<select type="number" [(ngModel)]="selectedItem">
              <option [disabled]="i.disabled" *ngFor="let i of items" [ngValue]="i.id">{{i.designator}}</option>
          </select>

working fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.