You can understand it in this way $rootScope is top most scope and all the other scope (i.e. $scope) comes inside this. So incase you have to pass value between scope then you have to use $rootScope.
Note: function1 use local variable Counter where scope is within the controller. I.e. each controller have there own scope. function2 use global variable Counter, because no global variable define.
In your case the (remember angular support two way binding, so whenever global value change it changes at all the places), The controllers get called in following order function1 -> function2 -> function1 -> function2.
When function1
get called first time $rootScope.Counter
is not present so it initialize with one and assign the same to local variable 'Counter'.
so output look like
Global value is 1
Child Instance of Function1 created :- 1
Child Instance of Function2 created :- 1
Child Instance of Function1 created :- NA // local scope for this is not created
Child Instance of Function2 created :- 1
Now function2 will get called. This increase global variable Counter to 2 and use it to display
so output look like
Global value is 2
Child Instance of Function1 created :- 1
Child Instance of Function2 created :- 2
Child Instance of Function1 created :- NA // local scope for this is not created
Child Instance of Function2 created :- 2
Now function1 will get called again. This increase global variable Counter to 3 and assign it local variable.
so output look like
Global value is 3
Child Instance of Function1 created :- 1
Child Instance of Function2 created :- 3
Child Instance of Function1 created :- 3 //Because rootScope already have counter variable with 2
Child Instance of Function2 created :- 3
and finally function2 get called again, This increase global variable Counter to 4 and use it to display
so output look like
Global value is 4
Child Instance of Function1 created :- 1
Child Instance of Function2 created :- 4
Child Instance of Function1 created :- 3
Child Instance of Function2 created :- 4