Factory vs Service

angularjs factory vs service do the same thing, just a different way of instantiation

by Nadeesh Peiris

HTML

<script src="https://code.angularjs.org/1.4.7/angular.min.js"></script>
<div ng-controller="HelloCtrl">
    <p>{{fromFactory}}</p>
</div>

JavaScript

var app = angular.module('myApp', []);

app.factory('decoratorService', function(){  
	/*caps = function(text) {
    return text.toLowerCase();
  }*/
  return {
  	//caps : caps
	}               
});

app.factory('testFactory', function(decoratorService){
	/*Decos = {
  	caps: function(text){
			return text.toUpperCase();
		}
  };*/
  
  World = function(w){
		this.name = w;
  }
  
  World.prototype.change = function(){
  	this.name = decoratorService.caps(this.name);
  }
  
	sayHello = function(text){
  	//var tempThis = this;
  	var w = new World(text);
    w.change();
		return "Factory says \"Hello " + w.name + "\"";
	}
  
	return {
    sayHello: sayHello,
    World: World
	}               
});

app.controller('HelloCtrl', function($scope, testFactory){
    $scope.fromFactory = testFactory.sayHello("World");   
});

app.decorator('decoratorService', function($delegate){

    $delegate.caps = function(text) {
			return text.toUpperCase();
    }

    return $delegate;
});

app.decorator('testFactory', function($delegate){
   /*
    $delegate.Decos.caps = function(text) {
			return text.toLowerCase();
    }
  */  
    $delegate.World.prototype.change = function(){
  		this.name = "Moon";
  	}

    $delegate.World = function(w){
			this.name = "Sun";
  	}

    return $delegate;
});