Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set default value on md-select component from angular 2 material design?

I have the following select component that gets populated from a data coming from a rest api. How Can I set default selected value on md-select?

  <md-select
                placeholder= "Warehouse"
                style="width: 100%"
                [(ngModel)]='selectedProductWarehouse.warehouse'
                name="Warehouse"
                required
                #Warehouse="ngModel">
          <md-option *ngFor="let warehouse of warehouses" [value]="warehouse">{{warehouse.description}}</md-option>
        </md-select>
like image 840
abdul.badru Avatar asked Jan 23 '17 17:01

abdul.badru


People also ask

How do you select a default value in mat select?

Just for information, you need to make sure the datatype of the default value matches with that of the options, for.eg, <mat-select [(value)]="heroes[0]. id"> <mat-option *ngFor="let hero of heroes" [value]="hero.id">{{ hero.name }}</mat-option> </mat-select> will work.

How to disable mat select in Angular?

Disabling the select or individual options This can be accomplished by creating a FormControl with the disabled property FormControl({value: '', disabled: true}) or using FormControl. enable() , FormControl. disable() .

What is mat option?

Each <mat-option> has a value property that can be used to set the value that will be selected if the user chooses this option. The content of the <mat-option> is what will be shown to the user. Angular Material also supports use of the native <select> element inside of <mat-form-field> .


3 Answers

you may try below,

Component HTML

  <md-select placeholder="Favorite food" [(ngModel)]="selectedValue" name="food">
    <md-option *ngFor="let food of foods" [value]="food.value" >
      {{food.viewValue}}
    </md-option>
  </md-select>

  <p> Selected value: {{selectedValue}} </p>

Component script

@Component({
  selector: 'select-form-example',
  templateUrl: './select-form-example.html',
})
export class SelectFormExample {
  foods = [
    {value: 'steak-0', viewValue: 'Steak'},
    {value: 'pizza-1', viewValue: 'Pizza'},
    {value: 'tacos-2', viewValue: 'Tacos'}
  ];


   // setting this is the key to initial select.
   selectedValue: string = this.foods[0].value;
}

The key here is setting selectedValue with the initial value.

Check this StackBlitz.

Hope this helps!!

like image 98
Madhu Ranjan Avatar answered Oct 14 '22 01:10

Madhu Ranjan


To set default value to md-select you need to set default value to the variable that you are using in [(ngModel)]. In your case:

Component Html =>

<md-select  
            placeholder= "Warehouse" style="width: 100%"
            [(ngModel)]='selectedProductWarehouse.warehouse'
            name="Warehouse"
            required
            #Warehouse="ngModel">
  <md-option *ngFor="let warehouse of warehouses" [value]="warehouse">
            {{warehouse.description}}
 </md-option>
</md-select>

Component Script =>

@Component({
  selector: 'select-form-example',
  templateUrl: './select-form-example.html',
})

constructor() {
  this.selectedProductWarehouse.warehouse = "default value"
}
like image 30
Vidur Singla Avatar answered Oct 13 '22 23:10

Vidur Singla


When you use Objects in md-option value, the object reference of the default value and the correponding option in the options list are not equal.

To fix this, you need to override the default value using the correponding option in the options list before setting the FormGroup.

Check this example

like image 1
abahet Avatar answered Oct 14 '22 00:10

abahet