Monday, December 28, 2015

Services vs Factories in angularjs

Hi Viewers,

Both are will be return object literal.

Services

Services is a constructor function

E.g :

app.service('myservice', function(name) {
  this.sayhello = function(name) {
    return "hi" + name + "!";
  }
});

app.controller('myController', function(myservice) {
$scope.name = "Nutty";
   myservice.sayhello($scope.name);
});

Factories

Factory function is really just a function that gets called, which is why we have to return an object explicitly.

E.g :

app.factory('myfactory', function(name) {

return {
   sayhello : function(name) {
      return "Hi " + name + "!";
    }
}
});

Thanks
Nutty

No comments:

Post a Comment