首页 > 解决方案 > 具有映射值的 Ng 自动完成过滤器列表功能

问题描述

我正在尝试像这样填充下拉列表:

this.service.loadProducts()
   .pipe(
        map(response => response.map(element => new Product(element)))
   )
   .subscribe(arr => this.products = arr);

是否有任何理由在 ng-autocomplete 过滤器列表功能中执行 item.constructor === 对象检查?

filterList() {
        this.selectedIdx = -1;
        this.initSearchHistory();
        if (this.query != null && this.data) {
            this.toHighlight = this.query;
            this.filteredList = this.data.filter((/**
             * @param {?} item
             * @return {?}
             */
            (item) => {
                if (typeof item === 'string') {
                    // string logic, check equality of strings
                    return item.toLowerCase().indexOf(this.query.toLowerCase()) > -1;
                }
                else if (typeof item === 'object' && item.constructor === Object) {
                    // object logic, check property equality
                    return item[this.searchKeyword].toLowerCase().indexOf(this.query.toLowerCase()) > -1;
                }
            }));
        }
        else {
            this.notFound = false;
        }
    }

由于该检查,过滤失败。有没有办法在 map 方法中不使用 return 语句来做到这一点?

标签: angularautocompleterxjs-observables

解决方案


推荐阅读