I have a dropdown which gets values from a service.
Dropdown
<mat-select
*ngIf="selectedCloudTypeName === 'AWS'"
class="select--width--130"
[formControl]="awsOwnersControl"
placeholder="AWS Owners"
msInfiniteScroll
(infiniteScroll)="loadMoreAwsOwners()"
[complete]="currentAwsOwnerssDropdownOffset >= awsOwnersTotalCount"
>
<mat-option *ngFor="let owner of awsOwners$ | async" [value]="owner.id">
{{ owner.name }}
</mat-option>
</mat-select>
Service call in TS file
getAwsOwners(offset = 0) {
this.isLoading$.next(true);
this.awsService
.getOwnersListForAws(this.selectedCredentialIdForCloudType, this.selectedPlatform, { offset, limit: 100 })
.subscribe(
(owners: PaginatedResult<CommonEnumValue[]>) => {
this.awsOwners.next(owners.data);
this.awsOwnersTotalCount = owners.totalCount;
this.isLoading$.next(false);
},
(error) => {
this.isLoading$.next(false);
this.alertify.error(error);
},
);
}
In OnInit hook I use an Observable
this.awsOwners$ = this.awsOwners.asObservable().pipe(scan((acc, curr) => [...acc, ...curr], []));
which I declare on as property in the file as
awsOwners = new BehaviorSubject<CommonEnumValue[]>([]);
awsOwners$: Observable<CommonEnumValue[]>;
The problem is when I call the getAwsOnwers function with different 'selectedPlatform' value, the old data persists and the new data gets appended.
I tried to clear the awsOwners Subject like this
this.awsOwners.next([]);
before I call it but it does clear it and new data gets appended to the old one,
Any ways how can I clear it?
I sense that in the scan operator I some how need to clear the '.acc' because it accumulates the values.