Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Services in AngularJS

    • 0
    • 3
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 2.82k
    Comment on it

    AngularJS within it self provides many services that we can use in our application, such as

    1. $http,
    2. $resource,
    3. $route,
    4. $window,
    5. $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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: