AngularJS within it self provides many services that we can use in our application, such as
- $http,
- $resource,
- $route,
- $window,
- $location etc
These Services are nothing but javascript functions defined inside Angular JS and they perform certain operations only. This makes them an individual entity which is maintainable and testable. Controllers, filters can call them as on requirement basis by injecting there dependency.
(Note: All angularjs predefined services starts with $ sign).
These services can be used within any Controller by just declaring them as dependencies.
For example:
module.controller('MyController', function($http){
// implementation of service
});
module.controller('IndexController', function($window){
// implementation of service
});
We can also define our own custom services in angular js app.
There are several ways to declare angularjs service within application.
Following are two simple ways:
var module = angular.module('myapp', [ ]);
module.service('MyCustomService', function(){
this.Products= ['Keyboard', 'Mouse', 'Monitor'];
});
0 Comment(s)