在角材料 2 的 md-select 中设置默认选项



我正在尝试md-select角度材料2设置默认选项,但无济于事。

form2.component.ts:

export class Form2Component implements OnInit {
    frequency = [
                {"title" : "One Time",         "description" : "", "discount" : {"value" : 0,  "type" : "value"} },
                {"title" : "Weekly",           "description" : "", "discount" : {"value" : 20, "type" : "percent"} },
                {"title" : "Every other Week", "description" : "", "discount" : {"value" : 15, "type" : "percent"} },
                {"title" : "Monthly",          "description" : "", "discount" : {"value" : 10, "type" : "percent"} }
            ]
    constructor(){}
    ngOnInit(){}
}

form2.component.html:

<md-select placeholder="Frequency" [formControl]="userForm.controls['frequency']">
    <md-option *ngFor="let frequ of frequency" [value]="frequ" [selected]="frequ.title == 'Weekly'">{{frequ?.title}}</md-option>
</md-select>

由于使用的是反应式窗体,因此可以将默认值设置为 form控件。因此,您可以从数组中找到所需的frequency并将其设置为默认值,例如:

this.userForm = this.fb.group({
  frequency: this.frequency.find(x => x.title == 'Weekly')
})

并从模板中删除selected

<form [formGroup]="userForm">
  <md-select placeholder = "Frequency" formControlName="frequency" >
    <md-option  *ngFor="let frequ of frequency" [value]="frequ" > {{frequ?.title}} </md-option>
  </md-select>
<form>

演示

组件 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>

组件.ts

@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;

将所选值设置为您希望其为默认值的数组中的值,您没有使用双向绑定

md-option 值中使用对象时,默认值的对象引用和选项列表中的对应选项相等

若要解决此问题,您需要在设置 FormGroup 之前使用选项列表中的相应选项覆盖默认值。

这是一个例子:

my.component.ts

export class MyComponent implements OnInit {
    myobject: Object = {id:'1323', label:'myform', service: {id:'S01', label:'Service 01': desc:''}}
    services: Array<Service> = [
        {id:'S01', label:'Service 01' , desc:'Service desc'},
        {id:'S02', label:'Service 02' , desc:'Service desc'},
        {id:'S03', label:'Service 03' , desc:'Service desc'}];
    myForm : FormGroup;
    constructor(private fb: FormBuilder) {
        // Override the service in  myobject before setting the FormGroup myForm
        this.services.forEach(srv => {
            // Compare service By id
            if (srv.id === myobject.service.id) {
                myobject.service = srv;
            }
        });
        // Set the formGroup with the myobject containing the good reference to the services array.
        this.myForm = this.fb.group(myobject);
    }
}

我的组件.html

<md-select class="carrier-select form-control" formControlName="service">
    <md-option class="carrier-option" *ngFor="let service of services" [value]="service">{{service.label}}</md-option>
</md-select>

最新更新