Angular 2 Component listen to change in service

Depending on how that boolean changes you could expose it as an Observable<boolean> on your service, and then subscribe to that stream in your component. Your service would look something like: @Injectable() export class MyBooleanService { myBool$: Observable<boolean>; private boolSubject: Subject<boolean>; constructor() { this.boolSubject = new Subject<boolean>(); this.myBool$ = this.boolSubject.asObservable(); } …some code that emits … Read more

How to conditionally inject service into component?

You can use the Injector import { Injector } from ‘@angular/core’ … constructor(private injector: Injector){ if(true) { this.oneService = <OneService>this.injector.get(OneService); } else { this.twoService = <TwoService>this.injector.get(TwoService); } } As @MeirionHughes mentioned this is called the service locator pattern: The technique is an example of the service locator pattern. Avoid this technique unless you genuinely need … Read more

How to subscribe to event emitter once?

A call to subscribe returns an instance of Disposable, which has a method dispose. Or if you are using RxJS 5, dispose has been renamed to unsubscribe (thanks @EricMartinez). And from the RxJS docs: …when we’re no longer interested in receiving the data as it comes streaming in, we call dispose on our subscription. Store … Read more

Static Methods and Angular 2 Services in JavaScript ES6

Static methods of a class, unlike instance methods, belong to (are visible on) the class itself (not an instance of it). They do not depend on the instance members of a class and will usually take input from the parameters, perform actions on it, and return some result. They act independently. They do make sense … Read more

Angular http.post without .subscribe callback

I do not think you can. http.post (and get, put, delete, etc) returns a cold Observable, i.e. an Observable for which: its underlying producer is created and activated during subscription Source. This means the function represented by the Observable is activated only with the subscribe() method. Convenience methods subscribe too, see implementation details for Observable#toPromise() … Read more

How to call component method from service? (angular2)

Interaction between components can be indeed achieved using services. You will need to inject the service use for inter-component communication into all the components which will need to use it (all the caller components and the callee method) and make use of the properties of Observables. The shared service can look something like this: import … Read more

Is it possible to use HostListener in a Service? Or how to use DOM events in an Angular service?

Seems like its not possible to use HostListener in a service. UPDATE like Stanislasdrg Reinstate Monica wrote, there’s a more elegant and more angular way using the renderer.. @Injectable() export class MyMouseService implements OnDestroy { private _destroy$ = new Subject(); public onClick$: Observable<Event>; constructor(private rendererFactory2: RendererFactory2) { const renderer = this.rendererFactory2.createRenderer(null, null); this.createOnClickObservable(renderer); } ngOnDestroy() … Read more

techhipbettruvabetnorabahisbahis forumuedueduseduedusedusedusedueduseduedus