2

I set a rootScope variable in my run block like so:

.run(['$rootScope', 'numbersService', function($rootScope, numbersService) {
  numbersService.getAvailableCodes().$promise.then(function(data) {
    $rootScope.availableCodes = data.codes;
  });
}]);

I need $rootScope.availableCodes to be available in order for my filter to run properly. Here is the beginning of my filter code:

.filter('formatValue', ['$rootScope', function($rootScope) {
  var availableCodes = $rootScope.availableCodes;
  ...

My problem is that since $rootScope.availableCodes is set based on an async call it is not guaranteed to be available when my filter runs (this filter appears in dozens of places throughout my HTML). How can I wait to run the rest of my filter logic until $rootScope.availableCodes is set?

MattDionis
  • 3,534
  • 10
  • 51
  • 105
  • duplicated [link](http://stackoverflow.com/questions/28609205/angularjs-promise-in-app-run) – Zamboney Mar 09 '16 at 17:02
  • A resolve in router would be best for this – charlietfl Mar 09 '16 at 17:02
  • @charlietfl, so move the code in my run block to a resolve within each route that needs access to it? – MattDionis Mar 09 '16 at 17:42
  • Would it be a lot of routes? – charlietfl Mar 09 '16 at 17:45
  • @charlietfl, it would be as this filter is used within several routes. I'm looking at answers such as this in order to build in some 'universal resolve' functionality: http://stackoverflow.com/questions/19937751/angular-how-use-one-resolve-for-all-the-routes-of-my-application – MattDionis Mar 09 '16 at 17:46
  • Which router are you using? If it's ui-router can have one resolve in a parent state and all child states will inherit that resolve. Can also check if value is set in filter... and return original input until it is set – charlietfl Mar 09 '16 at 17:48

0 Answers0