简体   繁体   English

Angular ng-select 触发(打开)事件时加载异步数据

[英]Angular ng-select load async data when (open) event is fired

I'm using ng-select custom server-side search to load data based on what the user types.我正在使用ng-select自定义服务器端搜索来根据用户类型加载数据。 Currently it only works if a keyword is actually pressed.目前,它仅在实际按下关键字时才有效。

<ng-select [items]="filterValues$ | async"
    [typeahead]="filterValuesInput$"
    [multiple]="true"
    (open)="getFilterValues(pref.id)"
    [loading]="filterValuesLoading"
    bindLabel="name"
    [(ngModel)]="filter_values">
</ng-select>

I want to trigger the API request when the select dropdown opens, even if no search term is provided.我想在 select 下拉菜单打开时触发 API 请求,即使没有提供搜索词。

getFilterValues(filterId) {
    this.filterValues$ = concat(
      of([]), // default items
      this.filterValuesInput$.pipe(
        distinctUntilChanged(),
        tap(() => this.filterValuesLoading = true),
        switchMap(term => this.service.getFilterValues(filterName, '' + term).pipe(
          map(res => res.filter_values),
          catchError(() => of([])), // empty list on error
          tap(() => this.filterValuesLoading = false)
        ))
      )
    );
}

Any suggestions?有什么建议么?

This is happening because the concat is not emitting any value cause of the second observable.发生这种情况是因为 concat 没有发出第二个 observable 的任何值原因。 You can try using startWith operator for the initial loading您可以尝试使用 startWith 运算符进行初始加载

getFilterValues(filterId) {
    this.filterValues$ = concat(
      of([]), // default items
      this.filterValuesInput$.pipe(
        startWith(''),
        debounce(300),
        distinctUntilChanged(),
        tap(() => this.filterValuesLoading = true),
        switchMap(term => this.service.getFilterValues(filterName, '' + term).pipe(
          map(res => res.filter_values),
          catchError(() => of([])), // empty list on error
          tap(() => this.filterValuesLoading = false)
        ))
      )
    );
}

Hope this helps希望这可以帮助

I have written similar code.我写过类似的代码。 I am getting this error in jasmine unit share.我在 jasmine 单元共享中收到此错误。 Could you please share test for the above code.您能否分享上述代码的测试。

    TypeError: Cannot read properties of undefined (reading 'length')
    error properties: Object({ ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 910941713, rootNodeFlags: 1, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 1, childFlags: 910941713, directChildFlags: 34097153, childMatchedQueries: 0, matchedQueries: Object({  }), matchedQueryIds: 0, references: Object({  }), ngContentIndex: null, childCount: 23, bindings: [ Object({ flags: 8, ns: '', name: 'hidden', nonMinifiedName: 'hidden', securityContext: 0, suffix: undefined }), Object({ flags: 2, ns: '', name: 'ng-untouched', nonMinifiedName: 'ng-untouched', securityContext: undefined, suffix: undefined }), Object({ flags: 2, ns: '', name: 'ng-touched', nonMinifiedName: 'ng-touched', securityContext: undefined, suffix: undefined }), Object({ flags: 2, ns: '', name: 'ng-pristine', nonMinifiedName: 'ng-pristine', securityContext: undefined, suffix: undefined }), Object({  ...
        at <Jasmine>
        at NgSelectComponent.get [as _isTypeahead] (test/test-index.js:210968:63)
        at NgSelectComponent../node_modules/@ng-select/ng-select/fesm5/ng-select.js.NgSelectComponent._setItems (test/test-index.js:210570:18)
        at NgSelectComponent../node_modules/@ng-select/ng-select/fesm5/ng-select.js.NgSelectComponent.ngOnChanges (test/test-index.js:210076:18)
        at checkAndUpdateDirectiveInline (test/test-index.js:64517:19)

HeadlessChrome 95.0.4638 (Mac OS X 10.15.7): Executed 17 of 432 (2 F HeadlessChrome 95.0.4638 (Mac OS X 10.15.7):执行 17 of 432 (2 F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM