Angular JS naming conventions ($, camelCase and PascalCase)

后端 未结 3 906
礼貌的吻别
礼貌的吻别 2020-12-15 17:08

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

相关标签:
3条回答
  • 2020-12-15 17:41
    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.

    0 讨论(0)
  • 2020-12-15 17:42

    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

    0 讨论(0)
  • 2020-12-15 17:52
    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;
            });
    
    0 讨论(0)
提交回复
热议问题