VOOZH about

URL: https://www.geeksforgeeks.org/angular-js/how-angularjs-prefixes-and-are-used/

⇱ How AngularJS prefixes $ and $$ are used? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How AngularJS prefixes $ and $$ are used?

Last Updated : 31 Jul, 2020
  • $:

    The $ in AngularJs is a built-in object.It contains application data and methods.

    The scope($) acts as a link between controller and view.

    👁 Image
    Scope($)

    Inside the controller function the properties and the methods to the scope($) can be attached. Expression, ng-model, or ng-bind directive can be used to display the scope data in the view.

    Output: 👁 Image
    Example 2: Output: 👁 Image

    $rootScope:

    The AngularJS application consists of a single $rootScope. All the other $scope are child objects.$rootscope has properties and methods attached to it which will be available be to all the controllers.

    Method

    Description

    $newIt is used to create new Child Scope
    $watchIt is used to Register a callback which is to be executed whenever the model property changes.
    $watchGroup

    It is used to register a callback  which is to be executed whenever model properties changes.

    We specify an array of properties in this.

    $watchCollectionIt is used to register a callback which is to be executed whenever model object or array property changes.
    $digestIt processes all of the watchers of the current scope and its children.
    $destroyRemove the current scope from parent scope.
    $evalExecute expressions on the current scope.
    $emitIt is used to dispatch the specified event upwards till $rootScope.
    $broadcastIt is used to dispatch the specified event downwards till child Scope.
    Example 3: Output: 👁 Image
  • $$ in this are treated as private variables. We use $$ to avoid the internal variable conflicts and not to expose for external use.

    Some of them are listed below:-

    $$observers, $$watchers, $$childHead, $$childTail, $$ChildScope etc.

    Method

    Description

    $$watchersIt contains all of the watches associated with the scope
    $$asyncQueueIt is a async task queue.It is consumed on every digest
    $$postDigest(fn)It executes fn after the next digest cycle.
    $$destroyedIt destroys the scope.

    Syntax:

    $$('.selector');

    or

    element.all(by.css('.selector'));

    A common ways of communication between modules of application, with the help of core AngularJS functionality is establishing a connection between controllers using $parent, $$childHead, $$nextSibling.

    Example: Output: 👁 Image

    The above output will be produced when we add $$, since, it acts as private object.

So, to prevent accidental name collisions while writing your code angular prefixes public objects with $ and private objects with $$.

Comment
Article Tags:

Explore