To set default selected item in Select with multip

2019-06-14 02:00发布

问题:

This question already has an answer here:

  • How to set default selected values in multiselect with ngModel in Angular 2 4 answers

I am using Select with multiple selection component.

I want to make 2 items to be selected by default like this.

I have surfed,but i got resources only for Basic select component, How can i achieve this?

回答1:

I have done it here please check https://stackblitz.com/edit/angular-zs2wa5?file=app/select-multiple-example.ts

export class SelectMultipleExample {
  toppings;
  toppingList: string[];

  constructor() {
    this.toppings = new FormControl();
    this.toppingList= ['Extra cheese', 'Mushroom', 'Onion', 'Pepperoni', 'Sausage', 'Tomato'];
    this.toppings.value = ['Mushroom', 'Onion', ];
    console.log(this.toppings);
  }
}