Wednesday, 30 August 2017

javascript - Ensure variable is available before running filter code

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?

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...