2

Hi , how do we set a default value for a select field using angular formly library

    this.uiFormFactory.createSelect(
          Location,
          this.store.select(StoreUtil.getParameterOptions(ParameterId.Location)),
          PersonDetailsFieldLabel.location,
          {
            required: true,
            select: { searchable: true, virtualScroll: true, defaultValue: 'Home' },
            readonly: false,
          }
        ),

1 Answer 1

3

you can use something like this:

  form = new FormGroup({});
  options: FormlyFormOptions = {};
  fields: FormlyFieldConfig[] = [
    {
      key: 'select box',
      type: 'select',
      defaultValue: 'value1',
      templateOptions: {
        label: 'select something',
        options: [
          { label: 'value 1', value: 'value1' },
          { label: 'value 2', value: 'value2' },
          { label: 'value 3', value: 'value3' },
        ],
      },
    },
  ];

  submit() {
    console.log('submit form');
  }
}
<form [formGroup]="form" (ngSubmit)="submit()">
  <formly-form [fields]="fields" [options]="options" [form]="form"></formly-form>
  <button type="submit" class="btn btn-primary submit-button" [disabled]="!form.valid">Submit</button>
</form>

of course, you can set the options array as you wish

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.