Posts

Showing posts from August, 2017

Subject in Angular 2

Image
Hello all, Here I am with my second piece of writing on JavaScript solutions to common problems. In this article we will look at Subject in Angular and its comparison with EventEmitter . First lets know what Subject is? Subject is an Observable. But an Observable only allows you to subscribe to an event whereas subject allows you to both publish and subscribe to an event. So Subject allows your service to be used as both a publisher and subscriber. Question: Now a Question always arise that why should we use Subject instead of EventEmitter provided by Angular itself whereas Subject is part rxjs package which is a third party provider. In my opinion EventEmitter is expected to emit events between components or directives. As per  EventEmitter Documentation  which says: use by directives and components to emit custom events .  Eventhough you can use it as you want, but I think you will be misusing it. So using  Subject is a better choi