Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Jonathan FernandesCourses Plus Student 22,784 Points
Angularjs: How to store your AJAX result in a service so that you can access it through multiple controllers
I have an application which a customer model. Due to the nature of the application, I need to access that model through multiple controllers. I want the data referenced in the controllers to all point to the same thing.
My initial thought was simply creating a customer variable on the $rootscope and calling the $rootscope variable on whatever controller I needed it on by simply injecting the $rootscope. Everything online says using the $rootscope is like setting a global variable which I know can be bad.
So my question is: if it is bad to set such a variable that I will potentially be changing and messing with throughout my application, then how should I make an AJAX call, set that to a variable in a service and inject it in my controllers so that they all reference the same variable?