Xich Top
Xich Top

Reputation: 21

EventEmitter subcriber subcribes multiple times with only one emit

I had a customService using EventEmitter like below. My problem is that every time I emit the value, the subcription in other component is called several times, looks like it emit multiple times. Is this expected behavior? How to prevent it?

The service is a singleton and I do not call emitValidation multiple times.

@Injectable()
export class customService {
    isValidation: EventEmitter<any> = new EventEmitter();
    emitValidation (data) {
        this.isValidation.emit(data);
    }

}

Subscription in component:

subVar: Subscription;
ngOnInit() {
    this.subVar = this.customService.isValidation
        .subscribe( data => {
            //Do stuff
            //Call multiple times here
        }
    });
}
ngOnDestroy() {
    if (this.subVar) {
      this.subVar.unsubscribe();
    }
}

Call in another component:

this.contactService.emitValidation(0);

Upvotes: 0

Views: 461

Answers (0)

Related Questions