Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does AngularJS resolves call to variables on $scope in 2 or more controllers?

Here, the author mentions

the $scope object used by the two controllers are not the same $scope object

Snippet for the same:

enter image description here

enter image description here

Now consider a little modification to the above code.

<body ng-app="myapp">
    <div ng-controller="myController1">

        <div>{{data.theVar}}</div>
        <div>{{data.common}}</div>

        <div ng-controller="myController2">
            <div>{{data.theVar}}</div>
            <div>{{data.common}}</div>
            <div>{{temp}}</div>
            <div>{{newTemp}}</div>
        </div>

    </div>
    <script>
        var module = angular.module("myapp", []);
        var myController1 = module.controller("myController1", function($scope) {
            $scope.data = { 
                theVar : "Value One",
                common : "common Value"
            };

            $scope.temp = "John Wick 2 is going to be released soon";
        });

        var myController2 = module.controller("myController2", function($scope) {
            $scope.data = { 
                theVar : "Value Two"
            };

            $scope.newTemp = $scope.temp;
            console.log("");

        });
    </script>
</body>

The view corresponding to controller2 has been moved inside the view for controller1.

For this piece of code inside the controller2,

$scope.newTemp = $scope.temp;

enter image description here

Are $scope highlighted above one and the same object?

If yes, how does AngularJS know this?

Had they been same, $scope.temp in controller2 would be undefined and so then $scope.newTemp?

For me, they are not the same, considering the o/p of the above program. See below:

enter image description here

But then, I am perplexed as to why they both comes out to be one & the same when I debug, enter image description here

How does AngularJS able to access value of $scope.temp from controller1 in controller2?
Please clarify?

Lastly,

enter image description here

like image 426
Farhan Shirgill Ansari Avatar asked Feb 10 '16 07:02

Farhan Shirgill Ansari


2 Answers

Altough it's true that the $scope used in two controllers are not the same, they can inherit eachother's properties. Angular's $scopes are like a tree, the trunk is the $rootScope and every other $scope branches from that or another $scope, so since your myController2 is a child of myController1 you can access the variables in it.

$rootScope -> myController1 -> myController2

The myController2 can access all the parent $scopes, the myController1 can access $rootScope and the $rootScope can only access itself.

like image 117
Martijn Welker Avatar answered Nov 18 '22 19:11

Martijn Welker


For your last part, as both controllers have property by name data, angular will look into current scope first and then hierarchically move up i.e. parent's scope. Therefore, angular founds data in second controller's scope itself and hence need not refer parent's scope data variable. But there is no common key inside that property and hence does not output anything out.

Do look into controller as syntax of angular, it is meant to keep these conflicts at bay.

like image 23
Aman Gupta Avatar answered Nov 18 '22 18:11

Aman Gupta