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.

Community
  • 1
  • 1
ankur
  • 557
  • 1
  • 10
  • 37

1 Answers1

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="angular.js@1.1.5" 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="angular.js@1.1.5" 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.

pedromarce
  • 5,651
  • 2
  • 27
  • 27
  • 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** – ankur Feb 18 '14 at 10:46
  • 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? – pedromarce Feb 18 '14 at 10:48
  • 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"] – ankur Feb 18 '14 at 10:50
  • 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... – pedromarce Feb 18 '14 at 10:53
  • 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](http://plnkr.co/edit/kBSwatl4GZFY1KckIc4f?p=preview) . Its still not working. Basically (from the plunker) My current situation is **"2"** . i.e. I cant change**ng-model="NotBinding"** to **ng-model="myservice.NotBinding"** – ankur Feb 18 '14 at 10:56
  • I've updated the answer and plunker with a $watch so you update values without binding the model to the service. – pedromarce Feb 18 '14 at 11:03
  • now here is the part. I know I could have added watches. but why is the question. i mean had it been local model values example ** js: $scope.val1 = $scope.val2 ** (the values in both of them would have been in sync all the time) . so when it comes to services ...why add a watch. i.e. why cant ** myservice = $scope.val2 [written once in controller] ** keep the service updated at all times – ankur Feb 18 '14 at 11:11
  • now i understand that i was missing some core concept, but just curious if you could throw some light on that because i still dont know what i was missing. – ankur Feb 18 '14 at 11:14
  • Because angular uses default watch functions for keep $scope updated, and ng-model will have this default watch bound. But unless you specifically changed the value in the service, angular won't do it for you. – pedromarce Feb 18 '14 at 11:16
  • can you post some link so that i could read a bit more and have more clarity on the same. I just started using angular 2 months back – ankur Feb 18 '14 at 11:20
  • http://docs.angularjs.org/guide/scope I think angular documentation is pretty clear about it. – pedromarce Feb 18 '14 at 11:40