SEARCH BY

All

  • All
  • Java8
  • Spring

Factory in AngularJS

Post a Comment

What is factory in AngularJS?

Factory is a resusable unit in AngularJS controller block.

(function() {
    'use strict';

    angular
        .module('Module')
        .factory('factoryName', factoryFuntionName);

        factoryFuntionName.$inject = ['dependency1'];
    function factoryFuntionName(dependency1) {
        var service = {
            exposedFn:exposedFn,
            exposedFn2:exposedFn2
        };
        
        return service;

        ////////////////
        function exposedFn() { 

            return "Some thing"; // if required
        }

        function exposedFn2() { 

            return "Some thing"; // if required
        }
    }
})();
(function () {
    'use strict';

    angular
        .module('cookiestore_app')
        .factory('priceCalc', priceCalcFun);

    // priceCalcFun.$inject = ['dependency1'];
    function priceCalcFun() {
        var facItem = {
            priceCalc: priceCalecu,
            nameDisplay: nameAndType
        };

        return facItem;

        ////////////////
        function priceCalecu(q, p) {

            return q * p; // if required
        }

        function nameAndType(n, t) {

            return "you bought: " + n + "In cat:" + t; // if required
        }
    }
})();
(function () {
    'use strict';

    angular
        .module('cookiestore_app')
        .controller('price', ControllerController);

    ControllerController.$inject = ['$scope', 'priceCalc'];
    function ControllerController($scope, priceCalc) {
        $scope.priceTotal = priceCalc.priceCalc(2, 5);
    }
})();
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox