Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is $rootScope and how is it related with $scope?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 114
    Comment on it

    $rootscope is the parent of all scopes. Values exposed there will be used in all controllers and templates. It acts as a parent object of all “$scope” angular objects created in a web page.

    Now, let us understand how the “$rootScope” and “$scope” hierarchy is created in angular.
    Suppose we multiple div and each div is tied with a controller:-

    <body ng-app = mainApp>
    {{ something }}
    <div ng-controller = firstCtrl
    </div>
    <div ng-controller = secondCtrl
    </div>
    <div ng-controller = thirdCtrl
    </div>
    </body>


    Step 1 :- Firstly, Angular parser encounters the ng-app directive and then creates a $rootscope object in memory.

    Step 2 :- Secondly, it creates a variable as the parser moves ahead and then finds the expression {{ something }}.

    Step 3 :- After this,the parser finds the “div” tag and looks for the attached “ng-controller” directive. This directive is pointing to “firstCtrl” controller.

    Step 4 :-Now the “ng-controller” directive creates a $scope object instance for the “firstCtrl” controller which is then attached to the “$rootscope” object.

    Step 5 :- Every time parser finds the “ng-controller” tag, it repeats step 3.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: