I have component review with array Reviews and component CreateReview with form to create new review. At this moment this two components are on this same page so i wish to could push new element from CreateReviewComponent on array from ReviewComponent to show all reviews with new one. How to do that?
1 Answer
You could have your array of reviews in a service, and in ReviewComponent display the reviews obtained in that service. And in form of the CreateReviewComponent, if all is ok, call the same service to add the new review to the list. If both components have the service injected, both of theme will share the same instance of the service and the list of reviews will be the same.
I hope this helps!
2 Comments
Maciek Drabicki
In this approach is problem (please correct me if i'm wrong) because application use multiple instance of service. So when i try to push my review i get error that my review's array is undefined,
Octavio Garbarino
sorry @maciek-drabicki I have just see your comment, no, if you do not create a new instance in the SAME tab or page, it will use the same instance of the service, I have an application with some shared services, like messaging, preloader, auth, etc. And is the same instance. You can also specify to create a new instance of the service in the following link it is well explained angular.io/docs/ts/latest/guide/dependency-injection.html :)