6 Tips for Getting the Most Out of Angular JS
Separate Your Data Services from Your ControllersIt’s good to keep your data services separate from your view controllers because it allows you to keep view state logic away from your business logic.
A data service is anything that talks to an external API. At Charge, we have a data service that fetches the user’s list of devices. The device data service is responsible for keeping track of the list of devices and where to post to create a new device. With all of the HTTP work happening one layer removed from the controller, the controller is freed up to focus on representing the state of the data to the user. Which makes your controllers smaller and more readable. It also has the benefit of making your controllers easier to test. Mocking an entire HTTP response takes a lot more time than mocking a list of objects or an error.
As with many of my articles, instead of learning a process from scratch, I’ve decided to interview industry experts. That way you, as the reader, get to hear directly from these experts and learn their strategies. With that in mind I reached out for help and was contacted by John Donley, a Software Engineer at Charge, a San Francisco software company. Here’s what he had to say: