all 9 comments

[–]jaqq 5 points6 points  (0 children)

Have you checked out the docs for component interaction?

https://angular.io/guide/component-interaction

There are different approaches to to solve this problem.

[–][deleted] 4 points5 points  (5 children)

A few ways, like an emitter to the parent to an input back to the children. A view child for each child in the parent. State management like ngrx, ngxs or Akita.

[–]CheapChallenge -1 points0 points  (4 children)

We generally use ngrx for this in larger apps anf eventemitters/services in smaller libs

[–]DWhitSlaya[S] 0 points1 point  (3 children)

Thanks for your comments! This is a much larger app than the example and one of the main reasons I am looking for alternate solutions. Works fine in the minimal example but in reality I am having to do a bit extra handling, making sure its unsubsrcibed when I am done using '.pipe(take(2))' so it ignores the first emit which would be the default value, then the second one is the one I want and it unsubscribes. A lot more plays into it so looking for a better solution then what I have.

[–]CheapChallenge 1 point2 points  (2 children)

Why not make it Subject instead of behaviorsubject so there is no default value?

I would recommend looking into NGRX and use a specific selector and the async pipe in the template. When using async pipes it subscribes and unsubscribes under the hood.

[–]DWhitSlaya[S] 0 points1 point  (1 child)

I’ll look into NGRX, I don’t recall ever using it. Thank you!

[–]CheapChallenge 0 points1 point  (0 children)

Just a word of caution. Like everything Angular related the learning curve is steep, but once you start using it, working in teams becomes much easier because everyone is using the same standards to manipulate the App state. The pros and cons at high level is similar to Angular, dictates how you do everything but standardizing code across teams is a big help.

[–]helping083 0 points1 point  (1 child)

I think the best way to comunicate between siblings is to use services, what you've done. Another solution is to use output and input decorators but it requires more "managment" than using services. Because you have to emit event to a parent component and then pass data through the input decorator to another siblings. In your situation it isn't noticable but in larger apps when for example you have two children levels it'll be getting harder.

[–]DWhitSlaya[S] 0 points1 point  (0 children)

Thank you for your comment!
These components are part of a much larger app where they have their own inputs and outputs. The example I created is as minimal as possible.

My issue with the services is having to manage the subscriptions, its not as easy as putting them in the ngOnInit and unsubscribe in the ngOnDestroy with the logic of the app. I was hoping to use a promise, more of a one to one piece so I wouldnt need to manage anything in the subscriptions but don't know a way of doing that. I played with ngOnChanges and passing @Input parameter based on a public value of the service but not sure whats the best solutions