Home Ask Login Register

Developers Planet

Your answer is one click away!

Схирисх Шреста February 2016

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.


STEVER February 2016

some hack like this should work:

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

Post Status

Asked in February 2016
Viewed 2,864 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life