24

What is the convention in AngularJS for prefixing providers with $? Should I prefix all custom services in my own code?

Looks like all things that come with angular have prefixed services, e.g. $http. Controllers, however, are not prefixed with $ in most articles. Also, all angular code comes with services named in camelCase, however I've also seen PascalCase in many blogs online. Which one is the convention?

2

3 Answers 3

22
  1. Use PascalCase for controllers and for functions which return a constructor function that's supposed to be newed, e.g. var user = new User(). Controllers in Angular are viewed as scope constructor functions--thus the PascalCase.

  2. Controllers should have Controller appended in their name. See http://demisx.github.io/angularjs/2014/09/14/angular-what-goes-where.html for naming examples.

  3. Use camelCase for everything else.

These follow important Javascript conventions that dev around the world got used to.

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

2 Comments

Add Ctrl at the end of controllers or not?
Good question, Dan. Add Controller at the end of controller name. Do not abbreviate. I've updated my answer. Also note, that standalone controllers will be gone in Angular 2.0 in favor of Web Components.
19

The docs state this convention for internal services, but also state you should not do it for your own services to reduce naming collisions.

http://docs.angularjs.org/guide/concepts#angular_namespace

Also, regarding camelCase, the docs say to use camelCase.

Angular uses name-with-dashes for attribute names and camelCase for the corresponding directive name

http://docs.angularjs.org/tutorial/step_00

4 Comments

It says for the directives, but how about services and factories and stuff like that?
In the docs, they use camelCase for most stuff (except for Controllers for whatever reason) so I would say that is the way to go.
I'm guessing because controllers are thought of as "classes" in the sense they are "newed" up for each use, wheras services are singletons and are typically expected to return an "instance". However nothing prevents you from returning a constructor function as a service so in that case it may make sense to use PascalCase to differentiate it.
And should the name end with Service? Isn't this too verbose?
-2
We can filter Text in CamelCase using following code 
 app.filter('camelCase', function(){
            var camelCaseFilter = function(input){
                    var words = input.split(' ');
                     for (var i = 0, len = words.length; i < len; i++)
                         words[i] = words[i].charAt( 0 ).toUpperCase() + words[i].slice(1);
                     return words.join(' ');
                 };
                 return camelCaseFilter;
        });

Comments

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.