清除角度可观察的阵列

时间:2020-08-14 15:40:14

标签: angular rxjs

如何从可观察阵列中删除所有项目?它是无限滚动的一部分

https://stackblitz.com/edit/infinite-scroll-firestore?file=app%2Fapp.component.html

 this.data = this._data.asObservable()
  .pipe(
    scan( (acc, val) => { 
      return this.query.prepend ? val.concat(acc) : acc.concat(val)
    })
  )

我想删除数据数组中的所有内容。 重置功能对可观察的数据没有任何作用 它只是从_data中删除

reset() {
  this._data.next([])
}

我也尝试将其设置为null,但是没有用

reset() {
  this._data.next(null)
}

即使我这样设置

this.data = new Observable 

2 个答案:

答案 0 :(得分:1)

下面的代码会将空的可观察数组分配给数据对象

this.data = of([]);

答案 1 :(得分:1)

您可以应用一些高阶函数来改变状态:

reset$
  • reset$.next();之后将清除您的状态:add$
  • add$.next(1);之后添加一个值会将值添加到您的状态:_data
  • 如果您的class Program { static void Main(string[] args) { Calcu8(); } static void Calcu1() { int single; for (int i = 0; i < 10; i++) { single = 5; Console.WriteLine(single + i); } } //Method Calcu2 has the same code as Method Calcu1 in ILSpy static void Calcu2() { for (int i = 0; i < 10; i++) { int single = 5; Console.WriteLine(single + i); } } //class type static void Calcu3() { for (int i = 0; i < 10; i++) { Student stu = new Student(); stu.Name = "Tim"; //not the same Console.WriteLine(stu.GetHashCode()); } } //class type static void Calcu4() { Student stu = new Student(); for (int i = 0; i < 10; i++) { stu.Name = "Tim"; //same Console.WriteLine(stu.GetHashCode()); } } //string static void Calcu5() { string str = string.Empty; for (int i = 0; i < 10; i++) { str = "Hello"; //same Console.WriteLine(str.GetHashCode()); } } //string static void Calcu6() { for (int i = 0; i < 10; i++) { string str = string.Empty; str = "Hello"; //same Console.WriteLine(str.GetHashCode()); } } //struct static void Calcu7() { Person per = new Person(); for (int i = 0; i < 10; i++) { per.Name = "Tim"; //same Console.WriteLine(per.GetHashCode()); } } //struct static void Calcu8() { for (int i = 0; i < 10; i++) { Person per = new Person { Name = "Tim" }; //same Console.WriteLine(per.GetHashCode()); } } 发出新值,它将覆盖当前状态