Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between the different services in AngularJS

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 152
    Comment on it

    AngularJs provide different services to organise and reuse the code multiple times in the app.
    These are the two services:
    1. Service
    2. Factory

    These services are singleton objects.
    Lets Discuss them:
    1. Service: Service are singleton objects in which we can add methods / functions or properties to its object with the help of "this" keyword.
    It has some business logics and useful functions and these can be called from anywhere in the app like in Controllers, filters, etc. This way we can split our app in different services and use these services whereever we want them. This make our App more stable and more testable.

    Syntax for service:

    app.service('newService', function () {
     this.functionA = function () {
    
     };
    
     this.functionB = function () {
    
     };
     });
    

    2. Factory: Factory is simply a function which add some logic before creating the object and returns the created object. Factory is a more flexable compared to service method.

    Syntax for factory:

    app.factory('newFactory', function () {
    
     var newObj = {};
     newObj.functionA = function () {
    
     };
    
     newObj.functionB = function () {
    
     };
    
     return newObj;
    });
    

 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: