Search code examples
angularjsangularjs-service

Data sharing between controllers in angular js


Hi I am new to Angularjs. I am learning how to share data between two controllers using dataservice. Looking at the tutorial I made my own program but it is not working. Can anyone suggest what I am doing wrong here?

<!DOCTYPE html>
<html>
  <head>
    <title>AngularJS Services</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.5/angular.min.js"></script>
  </head>
  <body>
    <div ng-app="dataServiceApp">
            <div ng-controller="ChildCtrl">
                    <h2>First controller</h2>
                    <button>+</button>{{Holder.value}}
                  </div>
                  <div ng-controller="ChildCtrl2">
                    <h2>Second controller</h2>
                    <button>+</button>{{Holder.value}}
                  </div>
    </div>
    <script>
        var myapp = angular.module("dataServiceApp",[]);
        myapp.factory('Holder', function() {
  return {
    value: 0
  };
});
myapp.controller('ChildCtrl', function($scope, Holder) {
  $scope.Holder = Holder;
  $scope.increment = function() {
    $scope.Holder.value++;
  };
});

myapp.controller('ChildCtrl2', function($scope, Holder) {
  $scope.Holder = Holder;
  $scope.increment = function() {
    $scope.Holder.value++;
  };
});
    </script>
  </body>
</html>

Solution

  • You have forgotten to register onclick listeners to the buttons:

    <button ng-click="increment()">+</button>{{Holder.value}}
    

    Hope this helps. Complete working example below:

    <!DOCTYPE html>
    <html>
      <head>
        <title>AngularJS Services</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.5/angular.min.js"></script>
      </head>
      <body>
        <div ng-app="dataServiceApp">
                <div ng-controller="ChildCtrl">
                        <h2>First controller</h2>
                        <button ng-click="increment()">+</button>{{Holder.value}}
                      </div>
                      <div ng-controller="ChildCtrl2">
                        <h2>Second controller</h2>
                        <button ng-click="increment()">+</button>{{Holder.value}}
                      </div>
        </div>
        <script>
            var myapp = angular.module("dataServiceApp",[]);
            myapp.factory('Holder', function() {
      return {
        value: 0
      };
    });
    myapp.controller('ChildCtrl', function($scope, Holder) {
      $scope.Holder = Holder;
      $scope.increment = function() {
        $scope.Holder.value++;
      };
    });
    
    myapp.controller('ChildCtrl2', function($scope, Holder) {
      $scope.Holder = Holder;
      $scope.increment = function() {
        $scope.Holder.value++;
      };
    });
        </script>
      </body>
    </html>
    

    p.s. I also fully agree with JB Nizet's comment: check whether you really need to learn AngularJS instead of Angular 2-7/VueJS/React.