【发布时间】:2021-09-11 03:12:34
【问题描述】:
我添加了一个表单数组来处理多行。为了使它与索引一起工作,我必须将我的定义从:shoppingCartList Observable<any[]>; 更改为 shoppingCartList: Observable<any[]>[] = [];,但是当我这样做时,它会在我的过滤器函数中抛出一个错误,即 missing the following properties from type 'Observable<any[]>[]': length, pop, push, concat。看起来是因为我正在调用服务来过滤列表。实现这一点的正确方法是什么:
.HTML
<mat-autocomplete #auto="matAutocomplete" [displayWith]="entry">
<mat-option *ngFor="let case of shoppingCartList[i] | async" [value]="case">
{{title}}
</mat-option>
</mat-autocomplete>
.TS
shoppingCartList: Observable<any[]>[] = [];
this.shoppingCartList[index] = arrayControl.at(index).get('name').valueChanges //ERROR: (property) DraftformComponent.filteredPrimaryCaseSerial: Observable<any[]>[]
Type 'Subscription' is missing the following properties from type 'Observable<any[]>
.pipe(debounceTime(this.debounceTime))
.subscribe(
(val) => {
this.filteredList= this.getFilteredlist(val); //ERROR: (property) DraftformComponent.filteredPrimaryCaseSerial: Observable<any[]>[]
Type 'Observable<any[]>' is missing the following properties from type 'Observable<any[]>[]
}
);
public getFilteredList(val: string): Observable<any[]> {
return this.myService.getListByValue(val);
}
【问题讨论】:
-
我不知道到底想做什么。您是否有自动完成组件,并且当字段更改时,您希望填充过滤选项以显示服务数据?
-
是的,没错,但是因为它在表单数组中,所以它变得很棘手。我基于这个例子:stackblitz.com/edit/…
-
我认为,如果您稍微重构一下代码,您正在尝试做的事情会容易得多。您可以尝试创建您的设置的堆栈闪电战吗?我会做到这一点,这样您就没有任何可观察到的要传递给自动完成功能。处理所有数据,然后在组件中为处理后的数据设置一个变量,然后在 *ngFor 中使用该变量。我认为你应该从那个开始
-
听起来您正在使用服务来过滤数组。我假设你正在做一个自动完成,从服务器获得接下来的几个建议,对吗?你不应该需要一个 observable
的数组。看看这个问题的第二个答案:stackoverflow.com/questions/57852645/… -
是的,这是正确的@ChadK,如果有意义的话,我必须添加一个数组才能跟踪 formArray 中多行的用户数据。我是基于this 示例
标签: javascript html angular angular-material mat-autocomplete