Angular Component Store Service

JS
S
JavaScript

Simple store service made with an observable.

1@Injectable()
2export class LessonSelectedService {
3
4    private _selected: BehaviorSubject<Lesson> = new BehaviorSubject(null);
5
6    public selected$ = this._selected.asObservable().filter(lesson => !!lesson);
7    
8    
9    select(lesson:Lesson) {
10         this._selected.next(lesson);
11    }
12}
13
14// App
15  ngOnInit() {
16      ....
17      this.lessonSelectedService.selected$.subscribe(lesson => this.selectLesson(lesson));
18  }

Created on 6/23/2019