1

I am confused about when to use a service and a function in a controller to perform something . What's the difference here? Am new to AngularJS.

If the explanation could be illustrated with the scenario of registering users in a website that would be great.

Thanks

3
  • Do you mean service vs factory? Commented Nov 22, 2015 at 19:48
  • viralpatel.net/blogs/angularjs-service-factory-tutorial Commented Nov 22, 2015 at 19:50
  • @Filype Not really. This is what I am confused about: assume am registering a user - user fills form and post ...do I use a signup service to validate input or a function in a controller to validate? After validating, I want to post data to database, do I use function in the same controller or factory? What's the difference or what should guide me in this selection Commented Nov 22, 2015 at 19:51

1 Answer 1

1

Although you can use a function to get/send data I would use a service to deal with all of it. Then it's simple enough to plug in any controller you like simply by calling your service and the associated getter/setter to retreive/send your data.

Sign up to request clarification or add additional context in comments.

3 Comments

Aha now a bit clearer...just to confirm...I can have a function that is called signup but within that function I will utilise services to do things such as check user exist or not, create account, etc. Correct ?
Yes, so say your service is called myService, and within it you have a setNewUser(data) function, you simply add your service as a dependency to your controller and call myService.setNewUser(data) passing your data to your controller which can then execute a function within your service to send data to your server.
Thanks for explaining this...appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.