Skip to content

Instantly share code, notes, and snippets.

@0x-r4bbit
Created March 11, 2013 08:27
Show Gist options
  • Save 0x-r4bbit/5132799 to your computer and use it in GitHub Desktop.
Save 0x-r4bbit/5132799 to your computer and use it in GitHub Desktop.
Breadcrumb Servive for angular apps.
angular.module('services.breadcrumbs', []);
angular.module('services.breadcrumbs').factory('breadcrumbs', ['$rootScope', '$location', function ($rootScope, $location) {
var breadcrumbs = [];
var breadcrumbsService = {};
$rootScope.$on('$routeChangeSuccess', function (event, current) {
var pathElements = $location.path().split('/'), result = [], i;
var breadcrumbPath = function (index) {
return '/' + (pathElements.slice(0, index + 1)).join('/');
};
pathElements.shift();
for (i = 0; i < pathElements.length; i++) {
result.push({name: pathElements[i], path: breadcrumbPath(i)});
}
breadcrumbs = result;
});
breadcrumbsService.getAll = function () {
return breadcrumbs;
};
breadcrumbsService.getFirst = function () {
return breadcrumbs[0] || {};
};
return breadcrumbsService;
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment