Welcome to Part 4 of this review of the Pluralsight course
Angular JS: Get Started by Scott Allen.
Scott has over 25 years of commercial software development experience across a wide range of technologies. He has successfully delivered software products for embedded, Windows, and web platforms.
To learn more about him, see my career strategies interview with him and other successful developers on Outlier Developer.
Angular JS: Get Started is the first course in the Angular JS Learning Path.
Also in this series:
Angular JS Services
The aim of this module is to give you a quick overview of Services in Angular JS. If you need a more in depth guide, see the Creating and Using Angular JS Services of the Angular JS Fundamentals course.
The Venn of Services
We’ve seen and used the $http service earlier on in this course. We’ve also seen the $scope service, and have had a module on directives.
Directives serve as an intermediary between the model and the view and helps them to work together.
Scott describes Services as a grab bag where everything goes which doesn’t fit in a model, view or directive.
Services can be used be either controllers, models or directives.
In this module we look at the timeout and interval services.
To make our app more interesting, we’ll be adding a countdown.
The user will have 5 seconds to search, and if the timer reaches zero it will automatically search for whatever is in the textbox at that moment.
Firstly, we initialize a countdown variable to the value 5. We then see how to decrement it once a second.
Angular provides services that wrap each of these: $timeout and $interval. Scott says these are much easier to unit test than the native functions, and are optimized for the Angular two-way data binding system.
We see how to use the $interval service in this lesson, and how to resolve the error
TypeError: undefined is not a function
Scott explains that we need to remember to include $interval as a parameter in our controller declaration.
Scott explains that Angular Services help keep the logic inside our controllers simple and easily testable.
Scott says the log service is useful for debugging and diagnostics whenever we run into trouble with Angular.
It has the following methods on it:
We see a demonstration of using this.
Scott says we generally want to keep Services away from directly interacting with UI pieces, but there are some exceptions.
As an example if we need to read or write the location in the browser’s address bar, we may want $location to talk directly to our view because it presents a simplified API for the component.
Scott describes three reasons why we might want to create our own custom service:
- Create reusable logic
- Create shared data
- Manage complexity
A GitHub Service
We revisit the revealing module pattern here as well as the $http.get method.
Scott explains a bit more about promises in Angular and how we can use them to simplify our main script.js file.
He warns that there’s about 24 different ways to register and configure a service with Angular, and recommends the simple approach of using the factory method on our angular module.
One particularly useful service is the routing service. We look at this in the final module of this course.