How do you share data between controllers?

Technology CommunityCategory: AngularJSHow do you share data between controllers?
VietMX Staff asked 3 years ago

Create an AngularJS service that will hold the data and inject it inside of the controllers.

Using a service is the cleanest, fastest and easiest way to test. However, there are couple of other ways to implement data sharing between controllers, like:

– Using events
– Using $parentnextSiblingcontrollerAs, etc. to directly access the controllers
– Using the $rootScope to add the data on (not a good practice)

The methods above are all correct, but are not the most efficient and easy to test.