Set default option in mat-select

后端 未结 11 1674
北恋
北恋 2020-11-30 08:31

I have a simple select option form field in my Angular material project:

component.html

  
    

        
相关标签:
11条回答
  • 2020-11-30 08:49

    Try this

    <mat-form-field>
        <mat-select [(ngModel)]="modeselect" [placeholder]="modeselect">
            <mat-option value="domain">Domain</mat-option>
            <mat-option value="exact">Exact</mat-option>
        </mat-select>
    </mat-form-field>
    

    Component:

    export class SelectValueBindingExample {
        public modeselect = 'Domain';
    }
    

    Live demo

    Also, don't forget to import FormsModule in your app.module

    0 讨论(0)
  • 2020-11-30 08:50

    Using Form Model (Reactive Forms)

    --- Html code--
    <form [formGroup]="patientCategory">
    <mat-form-field class="full-width">
        <mat-select placeholder="Category" formControlName="patientCategory">
            <mat-option>--</mat-option>
            <mat-option *ngFor="let category of patientCategories" [value]="category">
                {{category.name}} 
            </mat-option>
        </mat-select>
    </mat-form-field>
    

    ----ts code ---

    ngOnInit() {
    
            this.patientCategory = this.fb.group({
                patientCategory: [null, Validators.required]
            });
    
          const toSelect = "Your Default Value";
          this.patientCategory.get('patientCategory').setValue(toSelect);
        }
    

    With out form Model

    --- html code --
    <mat-form-field>
      <mat-label>Select an option</mat-label>
      <mat-select [(value)]="selected">
        <mat-option>None</mat-option>
        <mat-option value="option1">Option 1</mat-option>
        <mat-option value="option2">Option 2</mat-option>
        <mat-option value="option3">Option 3</mat-option>
      </mat-select>
    </mat-form-field>
    

    ---- ts code -- selected = 'option1'; Here take care about type of the value assigning

    0 讨论(0)
  • 2020-11-30 09:00

    This issue vexed me for some time. I was using reactive forms and I fixed it using this method. PS. Using Angular 9 and Material 9.

    In the "ngOnInit" lifecycle hook

    1) Get the object you want to set as the default from your array or object literal

    const countryDefault = this.countries.find(c => c.number === '826');
    

    Here I am grabbing the United Kingdom object from my countries array.

    2) Then set the formsbuilder object (the mat-select) with the default value.

    this.addressForm.get('country').setValue(countryDefault.name);
    

    3) Lastly...set the bound value property. In my case I want the name value.

    <mat-select formControlName="country">
       <mat-option *ngFor="let country of countries" [value]="country.name" >
              {{country.name}}
    
      </mat-option>
    </mat-select>
    

    Works like a charm. I hope it helps

    0 讨论(0)
  • 2020-11-30 09:00
    On your typescript file, just assign this domain on modeSelect on Your ngOnInit() method like below:
    
    
    
     ngOnInit() {
            this.modeSelect = "domain";
          }
    

    And on your html, use your select list.

    <mat-form-field>
            <mat-select  [(value)]="modeSelect" placeholder="Mode">
              <mat-option value="domain">Domain</mat-option>
              <mat-option value="exact">Exact</mat-option>
            </mat-select>
          </mat-form-field>
    
    0 讨论(0)
  • 2020-11-30 09:01

    Normally you are going to do this in your template with FormGroup, Reactive Forms, etc...

        this.myForm = this.fb.group({
          title: ['', Validators.required],
          description: ['', Validators.required],
          isPublished: ['false',Validators.required]
        });
    
    0 讨论(0)
  • 2020-11-30 09:03

    Try this:

    <mat-select [(ngModel)]="defaultValue">
    export class AppComponent {
      defaultValue = 'domain';
    }
    
    0 讨论(0)
提交回复
热议问题