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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 244
    Comment on it

    Hi All,

    In this we will discuss about factories in Angular JS, as Factory and Services plays an important role in Angular jS application as they allows us to put reusable code at a single place that can later on used in many other places like controller or even to other services and factories.

    So first of all, What is Factory ? and why do we use it ? and does angular js has some built-in factories ? and how can we create our own factory working with Angular Js and use those in your own apps.

    So to answers to this is, Yes Angular Js does supports the concept of factories and its does has some built-in factories. and a factory follows the Singleton pattern which mean that a single object up in memory shared among multiple components that performs re-usable task like.

    • Ajax calls
    • Business rules
    • Share data between controllers

    In Angular JS a factory is basically a function that accepts two parameter that is name of the factory and a factory function and it returns as Provider which takes the name, and factory function that we passed while creating the factory and and it puts that factory function to the $get.

    To create a custom factory, we can do that in following way :

    var app = angular.module('myApp', []);
    myApp.factory('myFactory', function clientIdFactory() {
      var obj = {};
      obj.GetUserName = function(){      
                        /* your ajax call goes here */
                        }
       return obj;
    });
    

    Happy Coding...

 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: