0

I am trying to select all options in a multiple select in Angular 6. I have tried a few different approaches, below is what I think should work, but does not.

.html:

<button (click)="selectAll()">Select All </button><br>
<select [(ngModel)]="selectedOptions" multiple size="3">
  <option *ngFor="let opt of possibleOptions">{{opt}}</option>
</select>

.ts:

selectedOptions = [];
possibleOptions = ["a","b","c","d","e","f"];

selectAll() {
  this.selectedOptions = this.possibleOptions;
}

Stackblitz: https://stackblitz.com/edit/angular-pc1hj4

Note: I know there are other components that can do multiple select. I am trying to get this working without resorting to a different component.

1 Answer 1

2

You forgot to specify the value of the options:

<option *ngFor="let opt of possibleOptions" [value]="opt">{{opt}}</option>
Sign up to request clarification or add additional context in comments.

1 Comment

That was it, thanks! I had it in there in an earlier iteration, but it got pulled out when trying to figure out why it wasn't working.

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.