Cannot set set Angular Material mat-select default option

Natalia

I use mat-select and try to set default value after options are loaded. My questions are:

1. Is it possible to set an items while populating the select list? Here is the config of y select list where I set the options:

  this.listControls = [
    {
      id: 'employee',
      type: 'select',
      options: this.listEmployees().pipe(
        map((list: PaginatedList) => {
          return list.items.map(x => {
            x.name = `${x.name} (${x.count})`;
            return x;
          });
        })
      )
    },
  ];

2. Is it possible to set default option (make one of the item selected) without using ngModel? I simply have an object value but I cannot make the it to be selected?

employee = {
    id: 100,
    name: 'Jonathan'
}

I also try to apply a filter to this.listControls[0], but as it is filled 'observable', it does not work:

const test = this.listControls[0].options.filter(x => x.id === 100);
Msk Satheesh

Try Using:

app.component.ts

import { HttpClient } from "@angular/common/http";
import { Component, OnInit } from "@angular/core";
import { map } from "rxjs/operators";

/** @title Select with 2-way value binding */
@Component({
  selector: "select-value-binding-example",
  templateUrl: "select-value-binding-example.html"
})
export class SelectValueBindingExample implements OnInit {
  selected = "option2";
  listControls: any = [];

  constructor(private http: HttpClient) {}

  async ngOnInit() {
    this.listControls = [
      {
        id: "employee",
        type: "select",
        options: this.listEmployees().pipe(
          map((list: any) => {
            return list.data;
          })
        ),
        value: this.listEmployees().pipe(
          map((list: any) => {
            return list.data[0].first_name;
          })
        )
      }
    ];

    this.listControls.map((item: any, index: any) => {
      this.listControls[index].value.subscribe((data: any) => {
        this.listControls[index]["value"] = data;
      });
    });
  }

  listEmployees() {
    return this.http.get("https://reqres.in/api/users?page=2");
  }
}

app.component.html

<div *ngFor="let item of listControls">
  <mat-form-field appearance="legacy">
    <mat-label>Select an option</mat-label>
    <mat-select [(value)]="item.value">
      <mat-option>None</mat-option>
      <mat-option
        *ngFor="let item of item.options | async"
        [value]="item.first_name"
        >{{item.first_name}}
      </mat-option>
    </mat-select>
  </mat-form-field>
</div>

Here the working stackblitz example: https://stackblitz.com/edit/angular-r4u37v-crowwk?file=src/app/select-value-binding-example.ts

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Set default option in mat-select

From Dev

How to set default option in select with Angular?

From Dev

Laravel - Select - set Default option

From Java

Angular Material: mat-select not selecting default

From Dev

How to set default `option` in indexed Angular `select` tag?

From Dev

Set default value of select to null option

From Dev

Can't set select option as default

From Dev

How to set my default option select?

From Dev

how to set a default value for select option

From Dev

How to write prompt to set default select option

From Dev

AngularJS - how to set default select option

From Dev

foreach select not set correctly default option

From Dev

AngularJS : how to create select option and set default select option?

From Dev

Set default value on md-select When Value is an Object Angular4 Angular-Material

From Dev

I cannot set default value in select list

From Dev

angular material select - managing large set of options

From Dev

Angular ng-options remove blank or set default option in special select settings

From Dev

Can't set a default value on select>option when using ngChange="myFunction()" in my angular app

From Dev

Angular Material md select option group default selected values

From Dev

Set a default value in a select dropdown using Angular

From Java

How to set default value in material-UI select box in react?

From Java

how to use ng-option to set default value of select element

From Dev

set default option in a select via ng-init on VALUE and not INDEX

From Dev

Set default option in select-element using php

From Dev

How do I set the default option for select with AngularJS?

From Dev

Set angular material drop down default value from the controller

From Dev

Angular Material 2 unable to set default active tab

From Dev

In Angular, on selected option set default values for other options

From Dev

Set compiler option to default In CMake

Related Related

  1. 1

    Set default option in mat-select

  2. 2

    How to set default option in select with Angular?

  3. 3

    Laravel - Select - set Default option

  4. 4

    Angular Material: mat-select not selecting default

  5. 5

    How to set default `option` in indexed Angular `select` tag?

  6. 6

    Set default value of select to null option

  7. 7

    Can't set select option as default

  8. 8

    How to set my default option select?

  9. 9

    how to set a default value for select option

  10. 10

    How to write prompt to set default select option

  11. 11

    AngularJS - how to set default select option

  12. 12

    foreach select not set correctly default option

  13. 13

    AngularJS : how to create select option and set default select option?

  14. 14

    Set default value on md-select When Value is an Object Angular4 Angular-Material

  15. 15

    I cannot set default value in select list

  16. 16

    angular material select - managing large set of options

  17. 17

    Angular ng-options remove blank or set default option in special select settings

  18. 18

    Can't set a default value on select>option when using ngChange="myFunction()" in my angular app

  19. 19

    Angular Material md select option group default selected values

  20. 20

    Set a default value in a select dropdown using Angular

  21. 21

    How to set default value in material-UI select box in react?

  22. 22

    how to use ng-option to set default value of select element

  23. 23

    set default option in a select via ng-init on VALUE and not INDEX

  24. 24

    Set default option in select-element using php

  25. 25

    How do I set the default option for select with AngularJS?

  26. 26

    Set angular material drop down default value from the controller

  27. 27

    Angular Material 2 unable to set default active tab

  28. 28

    In Angular, on selected option set default values for other options

  29. 29

    Set compiler option to default In CMake

HotTag

Archive