change in a variable outside angular scope

Beginner in Angular + looked at most of complex $watch, $digest and $apply threads around but I still can't make something work.

var masterArray = [obj1, obj2, ... ];

This variable is not inside any angular scope and on plain JS and gets updated by plain JS again outside angular's scope periodically by user's interactions.

How can we capture the change in the masterArray, and update the associated $scope as well.

  app.('mycontroller', ['$scope', function($scope){
        $scope.items = masterArray;
    }]);

Is there a way to update $scope.items with masterArray changes? This thing works only when I reload the page as masterArray is pushed to LocalStorage everytime it's updated.

Similar concern - my data variable (outside angular) arrives a little late with callback function from server in plain js or jquery but my angular scopes and variables are already defined during document load.

I couldn't find a way to update my angular scopes associated with those variables which during page load were null or empty are now updated with data.

Upvotes: 0

Views: 281

Answers (1)

Stepan Suvorov
Stepan Suvorov

Reputation: 26176

some hack like this should work:

  app.('mycontroller', function($scope){
      $scope.items = masterArray;
      $scope.$watch(function() {
          return masterArray;
      }, function(masterArray) {
          $scope.items = masterArray;
      });
  });

Upvotes: 1

Related Questions