Suggested Videos
Part 16 - ng-include directive in AngularJS
Part 17 - Consuming ASP.NET Web Service in AngularJS using $http
Part 18 - $http service in AngularJS
In this video we will discuss
1. What is a service in Angular
2. Why do we need services in an angular application
3. What are the benefits of using services
What is a service in AngularJS
Before we talk about what a service is in Angular. Let's talk about a service in web development.
If you have any experience developing web applications
1. You might have heard about Web Services and WCF Services
2. You might have also created objects that provide some services. For example, a Math object may provide services to add numbers.
So, a service in Angular is simply an object that provide some sort of service that can be reused with in an angular application. The angular service object has properties and methods, just like any other JavaScript object.
AngularJS has lot of built in services. We discussed two of the built in services - $http & $log, in our previous video. $http service is used to make AJAX calls. $log service is useful to log an object to the console, which is very useful when debugging applications. We can also create our own custom services, which we will discuss in a later video.
For now let's understand the need for services.
Why do we need services in an angular application
The primary responsibility of the controller is to build the model for the view. The controller should not be doing too many things. For example, if the controller also has the logic to compute Age from Date of Birth, it violates one of the SOLID principles i.e the Single Responsibility Principle. The Single Responsibility Principle states that an object should only have a Single Responsibility. So this kind a logic belongs in it's own service, which can then be injected into the object that needs that service.
In our previous video session, we have injected 2 of the angular built in services i.e $http and $log service into the controller function that needs them.
In general, if the logic with in your controller, is becoming too large or too complex, then it is time, to take a step back, and think if anything can be abstracted into it's own service.
Services can be used by controllers, directives and filters.
What are the benefits of using services
Reusability : In a service you usually have a logic that you want to reuse with in your entire application. For example, any time you want to make AJAX calls, you can use one of the built in angular service - $http, simply by injecting it into the object that needs that service. The application is also easier to maintain when the reusable components are encapsulated into their own services.
Dependency Injection : Another benefit of services, is that, they can simply be injected into controllers or other services that need them.
Testability : Since services are injected into controllers or other services that need them, it becomes very easy to test them. Depending on which service you are testing, you can pass mock implementations or real implementations. If you are new to unit testing and mocking, don't worry, we will be covering these in detail in our upcoming videos.
In our next video we will discuss, how to create a custom service, register and use it.
Part 16 - ng-include directive in AngularJS
Part 17 - Consuming ASP.NET Web Service in AngularJS using $http
Part 18 - $http service in AngularJS
In this video we will discuss
1. What is a service in Angular
2. Why do we need services in an angular application
3. What are the benefits of using services
What is a service in AngularJS
Before we talk about what a service is in Angular. Let's talk about a service in web development.
If you have any experience developing web applications
1. You might have heard about Web Services and WCF Services
2. You might have also created objects that provide some services. For example, a Math object may provide services to add numbers.
So, a service in Angular is simply an object that provide some sort of service that can be reused with in an angular application. The angular service object has properties and methods, just like any other JavaScript object.
AngularJS has lot of built in services. We discussed two of the built in services - $http & $log, in our previous video. $http service is used to make AJAX calls. $log service is useful to log an object to the console, which is very useful when debugging applications. We can also create our own custom services, which we will discuss in a later video.
For now let's understand the need for services.
Why do we need services in an angular application
The primary responsibility of the controller is to build the model for the view. The controller should not be doing too many things. For example, if the controller also has the logic to compute Age from Date of Birth, it violates one of the SOLID principles i.e the Single Responsibility Principle. The Single Responsibility Principle states that an object should only have a Single Responsibility. So this kind a logic belongs in it's own service, which can then be injected into the object that needs that service.
In our previous video session, we have injected 2 of the angular built in services i.e $http and $log service into the controller function that needs them.
In general, if the logic with in your controller, is becoming too large or too complex, then it is time, to take a step back, and think if anything can be abstracted into it's own service.
Services can be used by controllers, directives and filters.
What are the benefits of using services
Reusability : In a service you usually have a logic that you want to reuse with in your entire application. For example, any time you want to make AJAX calls, you can use one of the built in angular service - $http, simply by injecting it into the object that needs that service. The application is also easier to maintain when the reusable components are encapsulated into their own services.
Dependency Injection : Another benefit of services, is that, they can simply be injected into controllers or other services that need them.
Testability : Since services are injected into controllers or other services that need them, it becomes very easy to test them. Depending on which service you are testing, you can pass mock implementations or real implementations. If you are new to unit testing and mocking, don't worry, we will be covering these in detail in our upcoming videos.
In our next video we will discuss, how to create a custom service, register and use it.
Hi Venkat,
ReplyDeleteThank You very much for the useful information.
Could you please explain what is promise in AngularJS?
Hi Venkat,
ReplyDeleteI found your angularsjs video tutorial very helpful.
can you please record a video on how to remove # while creating Single Page Application without using .net framework,ie:simple html and javascript files.