如何解决清除角度可观察的阵列
如何从可观察阵列中删除所有项目?它是无限滚动的一部分
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
解决方法
下面的代码会将空的可观察数组分配给数据对象
this.data = of([]);
,
您可以应用一些高阶函数来改变状态:
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()); } }
发出新值,它将覆盖当前状态
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。