具有过滤器的材料自动完成功能不适用于异步数据

时间:2019-05-08 10:26:26

标签: angular angular-material

我已经为此networkx GitHub做过准备,我的真实应用中的情况非常相似。

数据的到达时间晚于 public static void DetectAndSaveCollision(string value, object lockObject) { var acquired = false; try { acquired = Monitor.TryEnter(lockObject); } finally { if (acquired) { Monitor.Exit(lockObject); } else { lock (lockObject) { CollisionCollection.Add(Thread.CurrentThread, value); } } } } 的显示/设置时间。

我已经尝试手动触发changeDetection,但这也不能解决问题。

有什么快速提示吗?

什么是重要的

当我单击有效的“示例”和“同步案例”时,我已经获得了最初关注的建议。但是,“异步情况”不是这种情况。我想要相同的行为。

2 个答案:

答案 0 :(得分:1)

它正在工作,但是由于出现错误而崩溃。

您只需要警惕null:

private _filter(value: string): string[] {
  const filterValue = value.toLowerCase();

  // Here this.options can be null, so you get error when you .filter on null
  return this.options ? this.options.filter(option => option.toLowerCase().includes(filterValue)) : this.options;
  }

答案 1 :(得分:1)

您需要像下面那样替换代码-

 options: string[] = [];
 @Input('options') set onOptions(options: string[]){
    this.options = options;
    this.initAutoComplete()
  }
  filteredOptions: Observable<string[]>;

  ngOnInit() {
    this.initAutoComplete();
  }

  initAutoComplete(){
    this.filteredOptions = this.myControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this._filter(value))
      );
  }
相关问题