AngularJS and its use of Dollar Variables

Does anyone know if the reasoning behind the use of dollar methods and variables in angularJS is to instruct angularJS to avoid checking those values when a digestion is going on? So, if angular comes across $scope.$value and $scope.value, then it will avoid checking the former since it's prefixed with a dollar character in its variable name?


Solution 1:

It is just a naming convention from the below snippet http://docs.angularjs.org/tutorial/step_05

'$' Prefix Naming Convention
You can create your own services, and in fact we will do exactly that in step 11. As a naming convention, angular's built-in services, Scope methods and a few other angular APIs have a '$' prefix in front of the name. Don't use a '$' prefix when naming your services and models, in order to avoid any possible naming collisions.

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

Angular Namespace
To prevent accidental name collision, Angular prefixes names of objects which could potentially collide with $. Please do not use the $ prefix in your code as it may accidentally collide with Angular code.

Solution 2:

There are a few times Angular ignores variables prefixed with the dollar sign:

  1. In Schumli's comment below, where json filters will not output them
  2. When using the {{ }} directive, angular will not show nested $ variables. For example this only displays the visible property.

    <div ng-init="n = { visible: 'foo', $ignore: 'bar' };">{{ n }}</div>
    
  3. Additionally when adding an explicit watcher on a scope object, changes to properties with a leading dollar sign of this object will not trigger the watcher. See this updated fiddle.

  4. angular.equals() ignores keys prefixed with $.

Solution 3:

The $ prefix denotes a variable, parameter, property, or method that belongs to the core of Angular.

Properties on objects that originate inside the framework, but are not actually part of the API, may begin with $ – or even $$ – to denote a private method or property. This is the same way the _ prefix is often used in other libraries.

It doesn't have any effect on the way code is interpreted by the runtime, although the framework itself may give it special meaning. Basically, it is a naming convention that says "You shouldn't mess with this".

Solution 4:

Not completely sure, but I believe AngularJS internals rely on manipulating these $-prefixed variables during the digest. Checking these variables would mean that the digest would never stabilize, since they may constantly change during each cycle of the digest.

Don't quote me on it though. :)