无法使用Angular获取提前输入建议

无法使用Angular获取提前输入建议,angular,Angular,在下面的代码中尝试获取预先输入的建议,会触发查询并发出响应,但不会填充自动建议。如果我们再次输入数据,就会出现自动建议,并且观察到此.testData.filter不是控制台中的功能 testData: any = []; model: any; fnPopulateDataLovs(){ this.http .get("/api/sampleAPI", { params: { name: (document.g

在下面的代码中尝试获取预先输入的建议,会触发查询并发出响应,但不会填充自动建议。如果我们再次输入数据,就会出现自动建议,并且观察到
此.testData.filter
不是控制台中的功能

testData: any = [];
  model: any;
  fnPopulateDataLovs(){
    this.http
      .get("/api/sampleAPI", {
        params: {
          name: (document.getElementById('typeahead-format') as HTMLInputElement).value
        },
      })
      .subscribe((res) => {
        this.testData = res;
      });
  }

  search: OperatorFunction<string, readonly string[]> = (text$: Observable<string>) =>
    text$.pipe(
      debounceTime(200),
      distinctUntilChanged(),
      map(term => term.length < 2 ? []
        : this.testData.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
    )
  
  ngOnInit() {

  }
testData:any=[];
型号:任意;
FnPowlatedAtalovs(){
这是http
.get(“/api/sampleAPI”{
参数:{
名称:(document.getElementById('typeahead-format')作为HTMLInputElement)。值
},
})
.订阅((res)=>{
this.testData=res;
});
}
搜索:运算符函数=(文本$:可观察)=>
文本$.pipe(
去BounceTime(200),
distinctUntilChanged(),
映射(term=>term.length<2?[]
:this.testData.filter(v=>v.toLowerCase().indexOf(term.toLowerCase())>-1.slice(0,10))
)
恩戈尼尼特(){
}
Html:



请小心
此上下文。您将它作为回调,因此您可能会有惊喜。
的上下文在箭头函数中不会更改@Random@SagarV这确实是理论,但我的一个应用程序上有相同的代码,
search
是一个箭头函数,但仍然可以看到
无法访问未定义的xxx
(因为
这个
上下文)在prod中触发。它大部分时间都有效,但有时无效,并且无法找到何时/为什么…@Random这可能是其他原因。你可以很容易地找到它。我在过去4年中从未遇到过这样的问题。@SagarV我同意,在prod日志中看到这些我也非常惊讶,但代码非常简单,我访问
this.foo
搜索
箭头函数中,错误是
无法访问未定义的foo
。在箭头函数中声明它时,
这个
不可能是未定义的。因此,我猜这可能是由于在将函数提供给子函数时,或者当组件正在销毁,或者idk…我将箭头函数更改为一个方法,并执行
this.search=this.search.bind(this)
,似乎效果更好。。。
<input id="typeahead-format"
 type="text" 
 class="form-control" 
 [(ngModel)]="model" 
 [ngbTypeahead]="search" 
 (keyup)="fnPopulateDataLovs()"
 />