In Rxjs scan operator, is it possible to remove an item from the internal accumulator created by the scan operator?

自作多情 提交于 2019-12-06 15:08:27

Emit the value once to add it, and emit it a second time to remove it. If you emit a third time it will be added back agan.

const uploadPicture = new Subject<UploadPicture>();

this.previewPictures$ = uploadPicture.pipe(
    scan<<UploadPicture, UploadPicture[]>(
      (pictures, newPicture) => pictures.includes(newPicture) ? pictures.filter(p=>p !== newPicture) : [...pictures, newPicture],
      []
    )
);

const pic = new UploadPicture();
this.uploadPicture.next(pic); // this will add the picture
this.uploadPicture.next(pic); // this will remove the picture

This can be done easily in the template.

<div *ngFor="let pic of previewPictures$ | async">
    <button (click)="uploadPicture.next(pic)">Remove</button>
</div>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!