0

Well, I'm having some problems updating a progress bar (which is in a directive) from a controller.

here are some code snippets: my directive:

angular.module('TestApp').directive('orderProgress', ['$window', OrderProgress]);

  function OrderProgress($window) {

    var directive = {
      link: link,
      restrict: 'A',
      templateUrl: 'OrderProgress.html',
      controller: 'ProgressController',
      replace: true
    };
    return directive;

    function link(scope, element, attrs) {}
  }

controller for directive:

function ProgressController($scope, progressNumberService) {
    $scope.progress = progressNumberService.getProgress();
  }

progressNumberService just hides the detail for the amount of "progress":

var progress = 20;
  var progressServiceInstance = {
    incProgress: function() {
      progress += 20;
    },
    decProgress: function() {
      progress -= 20;
    },
    getProgress: function() {
      return progress;
    }
  };

  App.value('progressNumberService', progressServiceInstance);

of course the controller:

function Controller($scope, progressNumberService) {
    $scope.nextStep = function() {
      progressNumberService.incProgress();
    };

    $scope.prevStep = function() {
      progressNumberService.decProgress();
    };
  }

I've created an example: http://plnkr.co/edit/LtY4ZUG591Kd3mUKEmEF?p=catalogue

So why doesn't the directive get the update from the 'Controller', when the Next/Prev buttons are pressed?

1 Answer 1

1

So your issue is that the value is being updated in your .value module, but your directive controller is never calling getProgress once the values are updated. I would suggest using $broadcast and $on to send a message saying that the progress was updated. I tested this and it seemed to do the trick.

Controller:

angular.module('TestApp').controller(controllerId2, ['$scope', '$rootScope', 'progressNumberService', ProgressController]);

function ProgressController($scope, $rootScope, progressNumberService) {
  $scope.progress = progressNumberService.getProgress();

  $rootScope.$on("event:progress-change", function() {
    $scope.progress = progressNumberService.getProgress();
  });
}

And change your .value to a factory so you can use $rootScope to broadcast

App.factory('progressNumberService', function($rootScope) {
  return {
    incProgress: function() {
      progress += 20;
      $rootScope.$broadcast("event:progress-change");
    },
    decProgress: function() {
      progress -= 20;
      $rootScope.$broadcast("event:progress-change");
    },
    getProgress: function() {
      return progress;
    } 
  }
});

Here is the updated Plunker DEMO

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

1 Comment

Thanks. That did work. My angular knowledge is not at that level (yet).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.