Factory and Service Overview in AngularJS

Dan Wahlin
A free video tutorial from Dan Wahlin
Developer, Google GDE and Microsoft MVP
4.3 instructor rating • 2 courses • 23,256 students

Learn more from the full course

AngularJS JumpStart with Dan Wahlin

Building Web applications? Learn to build robust Single Page Applications (SPAs) with the popular AngularJS framework!

05:32:50 of on-demand video • Updated June 2019

  • Learn the benefits of Single Page Applications
  • Learn and be able to explain the key components in Angular
  • Stay up-to-date on Angular with Google Developer Expert (GDE) Dan Wahlin
  • Teach HTML New Tricks with Directives
  • Build Controllers and Bind Data to Views
  • Use $scope for Data Binding
  • Understand the importance of two-way data binding
  • Understand the Role of Modules
  • Learn what routing is and why it's so important in Angular apps
  • Understand how to use Factories and Services
  • Learn how to "Jazz" up your SPAs with Animations
English Factories and services play an important role in AngularJS applications. So in this section, we're gonna focus on a few things. First off, what are factories and services? Why would you want to use them? Does AngularJS have some built in ones? Which, the quick answer there is yes, it does. And how do you create your own factories and services, and use those in your apps? As mentioned, factories and services play an important role. And AngularJS provides several of these out of the box that you can use, which I'll show you in just a moment. But in a nutshell all factories and services boiled down to what we call singletons. Now if you're not familiar with that term, a singleton is a single object up in memory that just kind of hangs around, and then other objects such as controllers can interact with that single instance. Now why would you want to use them. Well, you might want to put your Ajax calls in one particular place so that multiple controllers can reuse that code and you don't duplicate the code. You might have some business rules that are shared across controllers or other components in your app. Calculations, sharing data between controllers. All of these things and more would be good use cases for using factories or services. Now when it comes to working with factories and services you can create your own through a module. So up to this point you've already seen how a module can be used to configure routes, we've touched on filters and directives, and we've looked at controllers. Well, modules are also used to create factories or services, and then there's some related terms we'll talk about as well called constants and value. So we'll get to that a little bit later. You can write your own, or you can use some built-in factories or services. Now AngularJS provides many different factories and services out of the box, and they really just call them services regardless of what they are. But here's a quick look at some of the key ones that are built-in, and we'll talk about a few of these throughout this module. So when you want to make Ajax calls, you can use $http to do that. Time out can be used in much the same way as Window does set time out. $window is the angular way to get to the window object if you need to. Location will get you to the actual URL and different information about it such as the hash. You can get to the host, and those types of things. Q is used with asynchronous processes, such as ajax calls and others. Rootscope is used behind the scenes to actually create new scopes as we, for instance, used controllers. In addition to the time out that I showed earlier, we also have intervals. So if you'd like a repeating type of timer going, you can use $interval to do that. $filter can be used to programmatically get to any custom filters or others that Angular has, that you might wanna get to in a controller. And then finally, $log kind of does what it says. You can use this for general logging purposes. Now, this isn't all of the services by any means. But this is a few of the key ones that you might encounter. And we'll talk about a few of these throughout the module. So, now that we've looked at some of the fundamental concepts of services, factories, the fact that they're singletons, and what they're used for,. Let's dive in to how we get started creating and using these.