垫子步进器不适用于选择选项

时间:2019-09-07 17:29:44

标签: javascript angular ngx-formly

我正在将ngx formly与垫步进器一起使用。我可以获取表格,但是步进器没有显示下拉列表

步进器:

<form [formGroup]="form" (ngSubmit)="submit()">
  <mat-horizontal-stepper>
    <mat-step *ngFor="let step of steps; let index = index; let last = last;">
      <ng-template matStepLabel>{{ step.label }}</ng-template>
      <formly-form [form]="form.at(index)" [model]="model" [fields]="step.fields" [options]="options[index]">
      </formly-form>

      <div>
        <button *ngIf="index !== 0" matStepperPrevious class="btn btn-primary" type="button"
          (click)="prevStep(index)">Back</button>
        <button *ngIf="!last" matStepperNext class="btn btn-primary" type="button" [disabled]="!form.at(index).valid"
          (click)="nextStep(index)">Next</button>
        <button *ngIf="last" class="btn btn-primary" [disabled]="!form.valid" type="submit">Submit</button>
      </div>
    </mat-step>
  </mat-horizontal-stepper>
</form>

代码:

import { Component, OnInit } from '@angular/core';
import { FormArray, FormGroup } from '@angular/forms';
import { FormlyFormOptions, FormlyFieldConfig } from '@ngx-formly/core';

export interface StepType {
  label: string;
  fields: FormlyFieldConfig[];
}

@Component({
  selector: 'app-step-profile',
  templateUrl: './step-profile.component.html',
  styleUrls: ['./step-profile.component.css']
})
export class StepProfileComponent implements OnInit {
  activedStep = 0;
  model = {};
  steps: StepType[] = [
    {
      label: 'details',
      fields: [
        {
          key: 'somekey',
          type: 'select',
          templateOptions: {
            label: 'label',
            options: [
              { name: 'Iron Man', value: 'iron_man' },
              { name: 'Captain America', value: 'captain_america' },
              { name: 'Black Widow', value: 'black_widow' },
              { name: 'Hulk', value: 'hulk' },
              { name: 'Captain Marvel', value: 'captain_marvel' }
            ]
          }
        }
      ]
    }
  ];

  form = new FormArray(this.steps.map(() => new FormGroup({})));
  options = this.steps.map(() => <FormlyFormOptions>{});

  constructor() { }

  ngOnInit() {
  }

  prevStep(step) {
    this.activedStep = step - 1;
  }

  nextStep(step) {
    this.activedStep = step + 1;
  }

  submit() {
    alert(JSON.stringify(this.model));
  }

}

1 个答案:

答案 0 :(得分:0)

我通过执行以下操作来做到这一点:

     {
        key: 'something',
        type: 'select',
        defaultValue: "hulk",
        templateOptions: {
          label: 'label',
          options: [
            { name: 'Iron Man', value: 'iron_man' },
            { name: 'Captain America', value: 'captain_america' },
            { name: 'Black Widow', value: 'black_widow' },
            { name: 'Hulk', value: 'hulk' },
            { name: 'Captain Marvel', value: 'captain_marvel' }
          ],
          labelProp: "name",
          valueProp: "value"
        }
    }

因此,看起来您只需要添加labelProp和valueProp属性。