嗨,我们如何使用angular form库为select字段设置默认值?
this.uiFormFactory.createSelect(
Location,
this.store.select(StoreUtil.getParameterOptions(ParameterId.Location)),
PersonDetailsFieldLabel.location,
{
required: true,
select: { searchable: true, virtualScroll: true, defaultValue: 'Home' },
readonly: false,
}
),
你可以这样写:
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>
当然,你可以按照自己的意愿设置选项数组