1

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
  • it would be more efficient to post some codes Commented Dec 26, 2016 at 20:04

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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,
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.