Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

The 5 AngularJS 1 Services Explained in Simple and Concise Terms

4/17/2016

0 Comments

 
I sometimes wish I had a short and easy to understand explanation of when to use each service type in AngularJS. Here it is:

The Controller

A controller is not one of the 5 services, but it is something chained off the angular.module(..') in your Javascript files. The key thing to remember about controllers is that they are tied to specific segments of HTML. This should be as thin as possible, delegating as much work as they can to the 5 different services.

The Service Service

This service actually uses the "service" keyword in the code, and is the go-to for building helper classes, async handlers, and all-around singletons. Inside of a service you define properties and methods on this, and then you can just call them as public methods on the name of the service object that you inject into your controllers. 

The Factory​ Service

You use the factory when you need objects. When creating a factory, you define an object and return it. It's especially useful when you have an array full of object.s

The Provider Service

Providers are used when you want to make services and factories that are configurable. This is done with a config​ method. I rarely ever use these.

The Constant ​Service

The constants are for, well, when you want to use constants defined for your whole module! This could be a string, an array, anything really. The point is that it's an object that can't be set later.

The Value ​Service

This, like the constant service, is a ingle value or object. While in the constant service uses the constant keyword and is unable to be changed, the value service uses the value keyword and can be changed later.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Want FREE access to
    my daily stock tips 
    ​newsletters??
    Sign up here:

    - Triple Gainers
    - Rippers N' Dippers
    - Growingest Growers
    ​

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021