Skip to content

Instantly share code, notes, and snippets.

@ThomasHambach
Created June 3, 2016 02:27
Show Gist options
  • Save ThomasHambach/8845f281365103cd04afb91655f92dab to your computer and use it in GitHub Desktop.
Save ThomasHambach/8845f281365103cd04afb91655f92dab to your computer and use it in GitHub Desktop.
Angular breadcrumb with ui router resolve interpolation
(function () {
'use strict';
const template = '<ol class="breadcrumb">' +
'<li ng-repeat="c in crumbs">' +
'<a ui-sref="{{c.name}}">{{c.label}}</a>' +
'</li>' +
'</ol>';
/*@ngInject*/
function breadCrumbs($rootScope, $state, $interpolate) {
return {
scope: null,
restrict: 'A',
template: template,
link: function (scope, element) {
scope.crumbs = [];
$rootScope.$on('$viewContentLoaded', function (event, toState, toParams, fromState, fromParams) {
var current = $state.$current;
var crumbs = [];
while (current.parent) {
var label = current.self.breadcrumb ? current.self.breadcrumb.label : null;
if (label) {
let p = $interpolate(label);
crumbs.push({
label: p(current.locals.resolve.$$values),
name: current.self.name
});
}
current = current.parent;
}
scope.crumbs = crumbs.reverse();
});
}
};
}
module.exports = breadCrumbs;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment