Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Component listen to change in service

I've a simple question about change detection.

I have a component and a (global) service with a boolean inside. How can I make the component listen to that boolean and execute a function if that boolean changes?

Thanks!

like image 256
Han Che Avatar asked Mar 28 '16 11:03

Han Che


People also ask

How angular 2 detect changes?

How is change detection implemented? Angular can detect when component data changes, and then automatically re-render the view to reflect that change.

How does Angular detect changes in service?

The answer is using observables. To run the change detector manually: Inject ChangeDetectorRef service in the component. Use markForCheck in the subscription method to instruct Angular to check the component the next time change detectors run. On the ngOnDestroy() life cycle hook, unsubscribe from the observable.

How can I detect service variable change when updated from another component?

Move subscription to the service, and both components can access this value. If you need value only once, you can use it directly (like I did in sidebar. component); If you need to update something with this value it you can use getter (example in header. component).

What is changedetection Angular?

Angular change detection and runtime optimizationlink Change detection is the process through which Angular checks to see whether your application state has changed, and if any DOM needs to be updated. At a high level, Angular walks your components from top to bottom, looking for changes.


1 Answers

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 new values using this.boolSubject... } 

Then in your component you would have something like this:

@Component({...}) export class MyComponent {     currentBool: boolean;      constructor(service: MyBooleanService) {         service.myBool$.subscribe((newBool: boolean) => { this.currentBool = newBool; });     } } 

Now depending on what you need to do with that bool value you may need to do some other things to get your component to update, but this is the gist of using an observable. Note, you will want to unsubscribe from the myBool$ stream at some point to prevent memory leaks and unexpected side effects.

Another option is you use the async pipe within your template instead of explicitly subscribing to the stream in the constructor. That will also ensure the subscription is disposed of automatically. Again though, that depends on what exactly you need to do with the bool values.

like image 140
Sam Storie Avatar answered Sep 18 '22 23:09

Sam Storie