0

I am still a novice with angular. I have asked a question similar to this before, but it seems to be a different issue at work here.

I have two controllers and a factory sharing information between them. I have two separate divs using two different controllers to show / hide using ng=show;

HTML

<div id=main>
  <div ng-controller="Ctrl1">
    <div ng-show="var1">Hidden Stuff</div>
  </div>
  <div ng-controller="Ctrl2">
    <div ng-show="var1">More Hidden Stuff</div>
  </div>
</div>

Both use the same var for ng-show, shared by a factory

JS Factory

app.factory('Srvc', function($rootScope) {
    var Srvc = {};
        Srvc.var1;
    return Srvc;
});

JS Controllers

app.controller('Ctrl1', function($scope, Srvc) {
    $scope.var1 = false;
    if (user interacts with html in div with ng-controller="Ctrl1") {
        $scope.var1 = true;
        Srve.var1 = $scope.var1;
    }
});

app.controller('Ctrl2', function($scope, Srvc) {
    $scope.var1 = Srvc.var1;
    if ($scope.var1 === true) {
        update HTML in div with ng-controller="Ctrl2" 
        although I shouldn't need to do this really should I?
    }
});

So from what I can tell the factory works ok, the data is saved in the factory Srvc.var1. However when I pass the data true to Srvc.var1 I cannot seem to get Ctrl2 to 'trigger' and update its html with ng-show=true

myol
  • 8,857
  • 19
  • 82
  • 143

3 Answers3

1

So it seems I need to $watch the service for a change within the controller.

Original answer is here.

app.controller('Ctrl2', function($scope, Srvc) {
    $scope.$watch(function () {
       return Srvc.var1;
     },                       
      function(newVal, oldVal) {
          $scope.var1 = newVal;
    }, true);
});
Community
  • 1
  • 1
myol
  • 8,857
  • 19
  • 82
  • 143
1

One way to solve this problem without explicitly creating a watcher, is to wrap var1 in an object inside the service and then pass this object as a $scope variable for both Ctrl1 and Ctrl2 controllers.

DEMO

Javascript

  .factory('Svc', function() {
    var service = {
      data: {}
    };

    // If you perform http requests or async procedures then set data.var1 here
    service.data.var1 = true;

    return service;
  })

  .controller('Ctrl1', function($scope, Svc) {
    $scope.data = Svc.data;
  })

  .controller('Ctrl2', function($scope, Svc) {
    $scope.data = Svc.data;
  });

HTML

<div id="main">
  <div ng-controller="Ctrl1">
    <div ng-show="data.var1">Hidden Stuff</div>
    <button type="button" ng-click="data.var1 = true">Set data.var1 to true</button>
    <button type="button" ng-click="data.var1 = false">Set data.var1 to false</button>
  </div>
  <hr>
  <div ng-controller="Ctrl2">
    <div ng-show="data.var1">More Hidden Stuff</div>
    <button type="button" ng-click="data.var1 = true">Set data.var1 to true</button>
    <button type="button" ng-click="data.var1 = false">Set data.var1 to false</button>
  </div>
</div>
ryeballar
  • 29,658
  • 10
  • 65
  • 74
0

I think setting Var1 to $rootScope instead of Srvc should work, just call $scope.$root.$digest() after updating var1.

and use ng-show=$root.Var1 in view.

sss
  • 1,259
  • 9
  • 23