简体   繁体   中英

How to pre-select a value in angular2 material design drop down?

I am trying to pre-select a value in dropdowns of angular2 material design. I have done the following but that's not working

<label *ngFor="let service of serviceOptions">
    <b>{{service.serviceName}}</b>
    <br /><br />

      <md-select placeholder="Select Package" [(ngModel)]="service.selectedPackage" formControlName="packageName" size="30">
        <ng-container *ngFor="let package of (reactivePackages | async)"  >
          <md-option *ngIf="service.serviceId==package.serviceId" (click)="selectServicePackage(service, package)">
            {{ package.packageName}}
          </md-option>
        </ng-container>
      </md-select>
    <br />  <br />
  </label>

any help will be appreciated.

Add the value property binding inside <md-option> to assign the selected value to service.selectedPackage . Since you have [(ngModel)]="service.selectedPackage" , its value should match with the binding property of package .

Here's example of binding md-option to package.packageName assuming service.selectedPackage holds similar values.

<md-option [value]="package.packageName" 
           *ngIf="service.serviceId==package.serviceId" 
           (click)="selectServicePackage(service, package)">
  {{ package.packageName}}
</md-option>

Plunker demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM