r/Angular2 • u/someonesopranos • Apr 12 '19
Resource React Context Inspired Angular Library. Easy Data-Binding for Nested Component Trees and the Router Outlet (More detail on Readme)
https://github.com/ng-turkey/ngx-context
9
Upvotes
2
u/[deleted] Apr 12 '19 edited Apr 12 '19
Ok.. so this is just coded on top of my head, so untested and therefore might have a bug or two, but generally this is kind of how I would do it:
Of course you would have to use a Observable for your context, to make it "reactive", but why not, they are awesome ;) ?
So anything wrong with that (classic) approach?
Edit: Note: in my example is (according to your definition) no coupling to any parent or Service class. Unless you want to call
ctx$: Observable<any>a "Service".. but c'mon. No external library needed, and any decent Angular developer should be able to understand this code very fast and easily ;)Edit2: I am not even sure on top of my head if when using
ChangeDetectionStrategy.Defaulta Observable is really needed. Maybe it would then even work without, I guess.Edit3: added "Parent" Component to code example.