Leave some input select Multiple(Angular Material) pre-marked

Asked

Viewed 250 times

2

I am using Angular Material in my application, and it in turn has this list created to fill in my input:

lista: any[] = [
{
  name: 'Cursos',
  tribunais: [
    { value: 'RDC1'},
    { value: 'RDC2'},
    { value: 'RDC3'},
    { value: 'RDC4'},
    { value: 'RDC5'},
    { value: 'INF' }
  ]
}
]

And I have my HTML component, which renders this list:

<mat-form-field>
 <mat-select placeholder="Cursos" multiple>
  <mat-optgroup *ngFor="let group of lista" [label]="group.name" [disabled]="group.disabled">
  <mat-option *ngFor="let tribunais of group.tribunais" [value]="tribunais.value">
    {{tribunais.value}}
  </mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>

And he looks like this: inserir a descrição da imagem aqui

However I want to leave some pre-marked inputs when I start my component, as I do to leave marked some options pre-defined?

1 answer

2


As quoted in documentation, you can create a list with the results and you can already put the values of value included in that list.

public selecionados = [RDC1, RDC4];

and then make Binding, data connection with html, from the created list

<mat-select [(ngModel)]="selecionados " ...>
</mat-select>

Update:

public selecionados = ['RDC1', 'RDC4'];

Add single quotes to the list items as the resulting list should be string, since the values of each selected item is also a string. I made an example in stackbliz.

Uppdate 2:

To analyze the values of selected

{{ selecionados | json }}
<mat-select [(ngModel)]="selecionados " ...>
</mat-select>
  • 1

    I tried, it just doesn’t work that way

  • @Marcelohenriquedosreis tries in the new way

  • also did not give, I think that and because there is one go inside another, and he cannot associate ngModel with the value

  • Put ngModel and selected, then insert the interpolation into html {{ selecionado | json }} for you to analyze the contents of the selected ones. So you will know correctly the values of each select.

  • thus: [(ngModel)]="{{ selected | json }}" ? , pq I tried and gave error at angular

  • No. use ngModel as I did in reply. o {{ selecionado | json }} you will add in your HTML code just like you did with the tribunais.value. but you’ll put him on top of the mat-form-field

  • I did not understand the explanation, can make an update in the code to understand better?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.