0

I have started learning angular.js but I am kind of having trouble understanding the data binding and scopes.

Here is my Html file :

 <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"    />
    <script   src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js">   </script>
    <title>Egghead</title>
    </head>
<body data-ng-app="myApp">
1. <div>
    NAME : <input type = "text" name = "name" data-ng-model="data.name"/> 
    Typed Name : <b>{{data.name}}</b>
    <br><br>
    EMAIL: <input type = "email" name = "email" data-ng-model="data.email"/>
    Typed Email : <b>{{data.email}}</b>
</div>

2. <div data-ng-controller="firstController">
    NAME : <input type = "text" name = "name" data-ng-model="data.name"/> 
    Typed Name : <b>{{data.name}}</b>
    <br><br>
    EMAIL: <input type = "email" name = "email" data-ng-model="data.email"/>
    Typed Email : <b>{{data.email}}</b>
</div>

3. <div data-ng-controller="secondController">
    NAME : <input type = "text" name = "name" data-ng-model="data.name"/> 
    Typed Name : <b>{{data.name}}</b>
    <br><br>
    EMAIL: <input type = "email" name = "email" data-ng-model="data.email"/>
    Typed Email : <b>{{data.email}}</b>
</div>  

    <script type="text/javascript" src="app.js"></script>
</body>
</html>

Now when I have this module for my current html :

(function(){
    var myApp = angular.module('myApp', []);

    myApp.controller('firstController', function($scope, $rootScope) {

        /*$rootScope.data = {name : "root", email : "[email protected]"};
        $scope.data = {name : "Ishan", email : "[email protected]"};*/
    });


    myApp.controller('secondController', function($scope) {
        /*$scope.data = {name : "Divyan", email : "[email protected]"};*/
    });

})(); 

any changes that I make in any of the textbox for say the "name" input reflects and is bound everywhere. Example I make a change in the input text with name = "name" inside the second controller, the value in the text box for first also changes, but when I remove the comments from my javascript file, any change I make in the second controller's input box are not reflected in the first's input box. Why?

1
  • If you mean they are not reflected in the first as in firstController, is because the $scope is different. firstController has his own scope and secondController has his. If you want to tie them together from worst option to best option you can use $rootScope, nest controllers and call parent or just use a service Commented Dec 21, 2015 at 11:21

1 Answer 1

1

$rootScope is parent of all scopes, so if you assign property to it, it will be available everywhere in views. Every controller has $scope, you have two sibling controllers, so they have different scopes, if you want to share data between controllers, you shall use service. Like this:

myApp.factory('dataSrvc', function () {
    return {
        getData: function () {
            return {
                name : "Divyan",
                email : "[email protected]"
            };
        }
    };
});

Then inject it into controllers:

myApp.controller('firstController', function($scope, dataSrvc) {
    scope.data = dataSrvc.getData();
});


myApp.controller('secondController', function($scope, dataSrvc) {
    scope.data = dataSrvc.getData();
});

In this case changes in first controller will not reflect on second and vice versa, because getData returns new instance of object every time (with same data).

Sign up to request clarification or add additional context in comments.

2 Comments

So when I have nothing commented in my js file, I have 3 data objects. 1 at the root scope and two at the scope of the respective controllers. Updating any one of them just updates the respective data object. But when I have commented the code there is a single data object that is at the root scope that is being updated by everyone?
@IshanSoni yes, if you only have data in rootScope, both controller's views will use same data object so it will change everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.