0

I am new to Angular services/factory.

This is my simple angular service & factory. Both outputs the same.

//defining service
app.service('myService', function () {
this.name = '';
this.setName = function (custName) 
{
this.name = newName;
return this.name;
};
});

//defining factory
app.factory('myFactory', function () {
var serviceObj = {};
serviceObj.name = '';
serviceObj.setName = function (custName) {
serviceObj.name = newName;
};
return serviceObj;
});

I have gone few links to get understanding of service vs factory, but I couldn't get it clear.

For the above snippet, what should I use, factory or service.

In general scenario, when should I use factory or service?

Looking for some clear & concise answer.

aarti
  • 104
  • 1
  • 6
  • @tpsilva, I read that link but as I said I couldn't get. I am new to it, hence looking for some more clear explainaion – aarti May 16 '16 at 16:20
  • @aarti what don't you understand specifically? If I were you, I'd re-phrase your question, reference the current duplicate question, and ask specifically what you don't understand so people can guide you. This question is just a duplicate at this point and will be closed. – mariocatch May 16 '16 at 16:25
  • The link mentioned by @tpsilva is very clear, what didn't you understand? – Ricardo Pontual May 16 '16 at 16:26
  • @aarti look at the other answers on that link, the first one is the worst for a newbie trying to understand the concept b/c it doesn't have an illustrations on how to use it. – ngDeveloper May 16 '16 at 16:38
  • @ngDeveloper, Yes,was doing the same. Thank You – aarti May 16 '16 at 17:37

0 Answers0