Clear a observable array angular

Samudra Gogoi

How do I remove all the items from the observable array? It is part of an infinity scroll

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)
    })
  )

i want to remove everything in the data array. the reset functions didn't do anything to the data observable it just removes from the _data

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

I also tried setting it to null but it didn't work

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

Even if I set it like this

this.data = new Observable 
Jonathan Stellwag

You can apply several higher order functions to mutate the state:

const reset = () => (state) => [];
const add = (newValue) => (state) => [...state, newValue];
const overwrite = (newState) => (state) => newState;

const reset$ = new Subject();
const add$ = new Subject();
const data$ = this._data.asObservable()
...

const data = merge(
  reset$.pipe(map(reset)),
  add$.pipe(map(add)),
  data$.pipe(map(overwrite))
).pipe(
  scan((state, fn) => fn(state), [])
);
  • Nexting the reset$ will clear your state: reset$.next();
  • Nexting a value to the add$ will add the value to your state: add$.next(1);
  • If your _data emits a new value it will overwrite the current state

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Angular: normal array from observable

分類Dev

Observable of array with filter using Angular 5 with RxJS

分類Dev

Angular / RxJS Multicasting Observable of Observable

分類Dev

Clear array data in c

分類Dev

Angular - Observable data not ready?

分類Dev

Angular 4: Subscribing to Observable

分類Dev

Angular&Observable debounceTime

分類Dev

Angular 6:Observable HTTPClient

分類Dev

Angular observable not updating automatically

分類Dev

unable to subscribe the observable in angular

分類Dev

Angular 2 temlate with Observable

分類Dev

Angular 6 return Observable

分類Dev

Mobx Observable Array

分類Dev

Can not bind to an Observable array

分類Dev

Angular2 Observable <Array <Object >>を処理します

分類Dev

Angular 2ObservableからObservable []

分類Dev

Clear an Rx.Observable bufferCount with an event driven Timeout?

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Angular 2: Convert Observable to Promise

分類Dev

Angular2 ChangeDetection or Observable?

分類Dev

How to reload the observable http in Angular?

分類Dev

Angular jest, test if inside observable

分類Dev

Convert String to Array to Objects in Observable

分類Dev

Convert String to Array to Objects in Observable

分類Dev

how to add an object to an observable of array

分類Dev

Push object into array in the observable subscribe

分類Dev

Displaying contents of a knockout observable array

分類Dev

map from observable array to distinct string array

分類Dev

How to clear the data for a angular-chart