Skip to content

Instantly share code, notes, and snippets.

@cmbirk
Created January 13, 2015 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmbirk/a34b7020c3a15ada7ff0 to your computer and use it in GitHub Desktop.
Save cmbirk/a34b7020c3a15ada7ff0 to your computer and use it in GitHub Desktop.
Angular ui-router Loading Indicator
A fairly simple and generic solution would be determining currently resolved ui-view by element hierarchy.
Create a directive and assign it to your ui-view elements. Example:
<div ui-view state-loader>
<div ui-view state-loader></div>
</div>
The directive will use the $stateChangeStart events to decide whether current ui-view is the one being resolved and add relevant classes. Example:
angular.module('myApp')
.directive('stateLoader', function stateLoader() {
return {
restrict: 'A',
scope: {},
link: function (scope, element) {
scope.$on('$stateChangeStart', function (e, toState) {
if (element.parents('[ui-view]').length === toState.name.split('.').length - 1) {
element.addClass('loading-state');
}
});
scope.$on('$viewContentLoaded', function () {
element.removeClass('loading-state');
});
}
};
});
This works quite well for simple state configurations with nested views.
It probably isn't enough for more complicated configurations or states with multiple views.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment