我在 Angular 8 項目https://ng-bootstrap.github.io/#/components/typeahead/examples 中使用來自 ng-bootstrap 的 typeahead 元素。我正在使用庫示例中給出的函數,但如果我的輸入之一為空,我會收到錯誤消息。我的組件類中有一個變量,它是用一個函數構建的:searchAutofill = (text$: Observable<string>) => text$.pipe( debounceTime(200), distinctUntilChanged(), map(term => term.length < 1 ? [] : this.option.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 15)) )option 是一個字符串數組,例如:[ "tom", "bob", "foo", "emma", null, "john", "hello", "example",當函數達到空值時,它返回ERROR TypeError: "v is null"。如何更改我的代碼以接受/忽略空值?
當輸入為空時,ngbtypeahead 函數返回 TypeError
RISEBY
2021-12-23 18:06:09