2

I have nesting select element in a form and I only want each value selected once. How can I check if occupation.occupation exists in the selectedoccupations array and set [disabled]='' to true if it exists?

component

  selectedOccupations: any[] = [
    { occupation: 'dev' },
  ];

template

<tr>
          <td>
            <md-select formControlName="occupation" [ngModelOptions]="{standalone: true}" placeholder="Occupation" (change)="filterGuests($event, i)">
              <md-option [value]="null">Occupation</md-option>
              <md-option *ngFor="let occupation of occupationz" [value]="occupation.occupation" [disabled]="">
                {{ occupation.occupation }}
              </md-option>
            </md-select>
          </td>
        </tr>
1
  • Have you tried using a filter? Commented Aug 15, 2017 at 0:36

2 Answers 2

2

Make a call to a typescript function which returns the value for you!

<tr>
  <td>
    <md-select formControlName="occupation" [ngModelOptions]="{standalone: true}" placeholder="Occupation" (change)="filterGuests($event, i)">
      <md-option [value]="null">Occupation</md-option>
      <md-option *ngFor="let occupation of occupationz" [value]="occupation.occupation" [disabled]="isDisabled(occupation)">
        {{ occupation.occupation }}
      </md-option>
    </md-select>
  </td>
</tr>

And then in typescript:

function isDisabled(occupation: any):boolean{
    // if it exists... disabled == true;
    return selectedOccupations.includes(occupation.occupation); // note, i'm not certain if this should just be occupation.
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try [disabled] = "selectedOccupations.includes(occupation)" ?

This isn't very efficient, but shouldn't be all that bad.

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.