首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ngx-formly ng-选择formcontrol更改发射值

ngx-formly ng-选择formcontrol更改发射值
EN

Stack Overflow用户
提问于 2020-02-26 04:01:25
回答 1查看 3.1K关注 0票数 0

我有一个带有自定义typeahead字段的表单,使用ng-select多选。它当前发出一个键值对数组。我需要它发出一个包含正确值的数组。这似乎应该是一项简单明了的任务,但我正在努力想办法做到这一点。当我尝试不同的东西时,我会发布更新。我可以构建一个帮助器来截取模型并重新格式化它,但在formcontrol中必须有一种干净的方法来做到这一点。

typeahead.component.ts

代码语言:javascript
复制
@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);
  }
}

form-config.ts

代码语言:javascript
复制
   {
      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);
        }
      }
    }
EN

回答 1

Stack Overflow用户

发布于 2020-02-26 07:06:37

我认为,这应该由ng-select以某种方式完成,试着传递bindValue输入(查看他们的文档)。从形式上讲,我们依赖formControl来控制发出的值,解决方案是删除[formControl]="formControl"输入并依赖onChange事件,但不推荐:

代码语言:javascript
复制
  onChange(item: any) {
    this.formControl.setValue(item);
  }

另一种方法是使用parsers

代码语言:javascript
复制
export class KupTypeaheadComponent {
  defaultOptions: {
    parsers: [(value) => ...],
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60402308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档