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>Number of resolves: {{ iResolves }}</p>
    <p>Number of notifies: {{ iNotifies }}</p>
    <p ng-repeat="strMessage in aFromService">{{strMessage}}</p>
</div>

JavaScript

var app = angular.module('myApp', []);
    
app.service('testService', ["$q", "$interval", function($q, $interval){
    this.getData = function(){
        var oRetval = [2, 4, 6],
            oDeffered = $q.defer();
        
        $interval(function() {
            oRetval[0]++;
            oRetval[1]++;
            oRetval[2]++;
            oDeffered.notify(oRetval);
            //oDeffered.resolve(strData);
        }, 1000);
        
        return oDeffered.promise;
    };
}]);

function Ctrl($scope, testService)
{
    $scope.iResolves = 0;
    $scope.iNotifies = 0;
    $scope.aFromService = [];
    
    testService.getData().then(function() {
        $scope.iResolves++;
    }, null, function(aData) {
        $scope.aFromService = aData;
        $scope.iNotifies++;
    });
}