How are AngularJS prefixes $ and $$ used?

$$ prefix in AngularJS is used as a private variable, as it is responsible for preventing accidental code collision with the user code.

Whereas, $ prefix is used to define angular core functionalities such as variable, parameter, property or method, etc.

In AngularJS, the $ and $$ prefixes are conventionally used for specific purposes:

  1. $ Prefix:
    • The $ prefix is commonly used for built-in AngularJS services, such as $http, $scope, $timeout, etc. These services are part of the AngularJS framework, and using the $ prefix helps distinguish them from custom variables or services.

    Example:

    javascript
    myApp.controller('MyController', ['$scope', function($scope) {
    $scope.message = 'Hello, AngularJS!';
    }]);
  2. $$ Prefix:
    • The $$ prefix is reserved for AngularJS internal properties or functions. These are considered private and are not intended for regular use in application code. They might be used by AngularJS itself for managing its internal state.

    Example:

    javascript
    var internalVariable = angular.$$minErr('myModule');

It’s important to note that using the $$ prefix for your own variables or services is not recommended, as it may lead to naming conflicts with future versions of AngularJS or cause unintended behavior.