Factory vs Service

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

by mslocum

HTML

<div ng-controller="Ctrl">
    <p ng-repeat="strMessage in aFromService">{{strMessage}}</p>
</div>

JavaScript

//angular.js example for factory vs service
var app = angular.module('myApp', []);
    
app.service('testService', ["$q", "$timeout", function($q, $timeout){
    this.getArray = function(){
        var oRetval = [],
            oDeffered = $q.defer();
        
        $timeout(function() {
            var strData = 'Data from service',
                strData2 = 'More data from service'            
            oRetval.push(strData);
            oRetval.push(strData2);
            oDeffered.resolve([strData, strData2]);
        }, 1000);
        
        oRetval.$promise = oDeffered.promise;
        return oRetval;
    };        
}]);

function Ctrl($scope, testService)
{
    $scope.aFromService = testService.getArray();
}