我有一个使用 ng-select 多选的自定义预输入字段的正式表单。它当前发出一个键值对数组。我需要它来发出一个值数组。似乎这应该是一个大致直截了当的任务,但我正在努力想办法做到这一点。当我尝试不同的事情时会发布更新。我可以构建一个助手来拦截模型并重新格式化它,但必须有一种干净的方式来在表单控件中做到这一点。
typeahead.component.ts
@Component({
selector: 'app-kup-typeahead',
template: `
<ng-select
[items]="options$ | async"
[ngClass]="{'ng-select-required': to.required}"
[placeholder]="to.label"
[typeahead]="search$"
[formControl]="formControl"
[multiple]="to.multiple"
(change)="onChange($event)"
>
</ng-select>
`,
})
export class KupTypeaheadComponent extends FieldType implements OnInit, OnDestroy {
onDestroy$ = new Subject<void>();
search$ = new EventEmitter();
options$;
ngOnInit() {
this.options$ = this.search$.pipe(
takeUntil(this.onDestroy$),
startWith(''),
filter(v => v !== null),
debounceTime(200),
distinctUntilChanged(),
switchMap(this.to.search$),
);
this.options$.subscribe();
}
ngOnDestroy() {
this.onDestroy$.complete();
}
onChange(item: any) {
console.warn('onChange ', item);
}
}
表单-config.ts
{
key: 'genotype.ploidy',
id: 'filter_ploidy',
type: 'kup-typehead',
templateOptions: {
label: 'Filter by Ploidy',
multiple: true,
options: of(res['creation_method']),
search$: (term: string) => {
return this.dropdownService.getDropdown('genotypes/ploidies', '', '', term);
}
}
}