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); } })();
Post a Comment
Post a Comment