0

Sharing between controllers through services

I have already gone through the links and videos

sharing data between controllers

similar sharing service

egghead.io link


Through my problem is little weird

I have a live working plunker for the same see plunker here


Problem description :

JAVASCRIPT

var testModule = angular.module('testmodule', []);

testModule
.controller('QuestionsStatusController1',
['$rootScope', '$scope', 'myservice',
function ($rootScope, $scope, myservice) {
   $scope.myserviceCopy = myservice;   
    $scope.myserviceCopy.newValue = $scope.NotBinding;

    myservice.confirming = "asdsadasdd";

    $scope.ForceBinding = function(){
        $scope.myserviceCopy.newValue = $scope.NotBinding;
    };

}]);

testModule
.controller('QuestionsStatusController2',
['$rootScope', '$scope', 'myservice',
function ($rootScope, $scope, myservice) {
  $scope.myservice = myservice;
 $scope.newMyService = myservice;
}]);

testModule
.service('myservice', function() {
  this.xxx = "yyy";
});
  • (in the plunker) "1" is working fine and updating instantly

  • "2" is only updating when i press the bindNow button

  • "3" is not updating at all

I just want all of them to refresh instantly and I dont want to use the "1" way(in the plunker)


I know I must be missing something that is conceptually different from what I have perceived.

1

1 Answer 1

0

I am not sure why you were creating some many copies of the service I have simplified your code and I think it should work as you expect:

js

var testModule = angular.module('testmodule', []);

testModule
   .controller('QuestionsStatusController1',
    ['$rootScope', '$scope', 'myservice',
    function ($rootScope, $scope, myservice) {
       $scope.myservice = myservice;   
        $scope.myservice.confirming = "asdsadasdd";
        $scope.ForceBinding = function(){
        $scope.myservice.newValue = $scope.NotBinding;
    };
    $scope.$watch("NotBinding",
        function( newValue, oldValue ) {
          $scope.myservice.newValue = newValue;
        }
    );
    $scope.$watch("confirming",
        function( newValue, oldValue ) {
          $scope.myservice.confirming = newValue;
        }
    );
    }]);

testModule
   .controller('QuestionsStatusController2',
    ['$rootScope', '$scope', 'myservice',
    function ($rootScope, $scope, myservice) {
      $scope.myservice = myservice;
    }]);

testModule
    .service('myservice', function() {
      this.xxx = "yyy";
    });

and html:

<!DOCTYPE html>
<html ng-app="testmodule">

  <head>
    <script data-require="[email protected]" data-semver="1.1.5" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div data-ng-controller="QuestionsStatusController1">
     1<br> <input ng-model="myservice.xxx" />{{myService.xxx}}<br>
      <hr>
     2<br> Dynamic Binding :       <input ng-model="NotBinding"/><br>
      Click to dynamic Bind <input type="button" value="bindNow" ng-click="ForceBinding()" >
    <hr>

    3<br>Binding directly to the service model<input ng-model="myservice.confirming">
    </div>
    <hr><hr><hr><hr><hr>

    <div data-ng-controller="QuestionsStatusController2">
   The value of xxx is: {{ myservice.xxx }}
   <hr>
   Dynamic Binding Problem Value  : {{myservice.newValue}}
   <hr>
   Direct binding to the service object  : {{myservice.confirming}}
    </div>
  </body>

</html>
<!DOCTYPE html>
<html ng-app="testmodule">

  <head>
    <script data-require="[email protected]" data-semver="1.1.5" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <div data-ng-controller="QuestionsStatusController1">
     1<br> <input ng-model="myservice.xxx" />{{myService.xxx}}<br>
      <hr>
     2<br> Dynamic Binding :       <input ng-model="NotBinding"/><br>
      Click to dynamic Bind <input type="button" value="bindNow" ng-click="ForceBinding()" >
    <hr>

    3<br>Binding directly to the service model<input ng-model="confirming">
    </div>
    <hr><hr><hr><hr><hr>

    <div data-ng-controller="QuestionsStatusController2">
   The value of xxx is: {{ myservice.xxx }}
   <hr>
   Dynamic Binding Problem Value  : {{myservice.newValue}}
   <hr>
   Direct binding to the service object  : {{myservice.confirming}}
    </div>
  </body>

</html>

Check the differences between your code and mine and you'll find what was wrong, for instance the div for the controller 1 was closed before number 3 (that was why it wasn't being updated).

You can see it in this plunkr.

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

11 Comments

The actual problem is that I am way too ahead with my code and now I feel the need to share data through services. I just want the way "2" to work instantly >> I do not want to press the bind now button
Correct, but the code you posted had some issues that is why is not working, the one I posted is sharing those values using the service as you asked, or am I missing something?
In a nutshell : I dont want to prefix my model that is right now [ng-model="localModalBoundToMyScope"] with "myservice"(as you have corrected in my plunker)... I cant change every occurrence of [ng-model="localModalBoundToMyScope"] with [ng-model="myservice.localModalBoundToMyScope"]
But then your model is saved in the local $scope, not the service, I guess what you could try is to add a watch to the value in your model and change the service property there. If that makes sense...
the one you have posted is working with way 1 and way 3 but its because you changed the service instances with "myservice". Let me give you my plunker with the correct div placement . link . Its still not working. Basically (from the plunker) My current situation is "2" . i.e. I cant changeng-model="NotBinding" to ng-model="myservice.NotBinding"
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.