Rxjs subject triggers twice in Angular

50 Views Asked by At

I have a subject search2Sub$ that is being emitted twice. I trigger it only one time in component1 after user stopped typing. Could youp please help figuring out what am I doing wrong?

appService
search2Sub$ = new Subject<boolean>()

component1
ngOnInit(): void {
    this.appService.search1Sub$.pipe (
        debounceTime(2000), 
    ).subscribe(value => {
        this.appService.search2Sub$.next(true)
    });
}

component2
this.appService.search2Sub$.subscribe(data=>{
    console.log(value)
})
ngOnDestroy(): void {
    this.appService.search2Sub$.unsubscribe()
}
1

There are 1 best solutions below

0
lili On

The resolution was saving as subscription and then unsubscribe from it

subscription = this.appService.search2Sub$.subscribe(data=>{
    console.log(value)
})

if (subscription) {
subscription.unsubscribe()
}

rxjs take operator didn't do a trick.