Skip to content

Instantly share code, notes, and snippets.

@justinwinslow
Last active December 31, 2015 13:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save justinwinslow/7994944 to your computer and use it in GitHub Desktop.
Save justinwinslow/7994944 to your computer and use it in GitHub Desktop.
Angular ui-router breadcrumbs
// State example:
//
// $stateProvider
// .state('state', {
// url: 'state/:id',
// template: stateTemplate,
// controller: stateController,
// // Expose parameters in display names using {:param} syntax
// displayName: 'State ({:id})'
// });
// Create Breadcrumb module
angular.module('Breadcrumb', [])
// Controller for module
.controller('BreadcrumbsCtrl', function($scope, $state, $stateParams) {
$scope.$on('$stateChangeSuccess', function(){
// Add the current state and params to the scope
$scope.current = $state.$current;
$scope.params = $stateParams;
});
})
// Create view
.directive('breadcrumbs', function(){
return {
// NOTE - you probably shouldn't have templates in your JS
// This is just to simplify this example
template: [
'<ul class="breadcrumbs">',
'<li ng-repeat="state in current.path">',
'<a href="{{ state.url.format(params) }}" ui-sref="{{ state.name }}" crumb></a>',
'</li>',
'</ul>'
].join('')
};
})
.directive('crumb', function(){
return {
// Inherit parent scope
scope: true,
template: '{{ name }}',
// Use the link function to do any special voodoo
link: function (scope, element, attrs) {
var getDisplayName = function(){
// Use displayName if provided else use default name property
var displayName = scope.state.displayName || scope.state.name;
// Loop through ownParams and replace any expressions with the matching value
angular.forEach(scope.state.ownParams, function(param, index){
displayName = displayName.replace('{:' + param + '}', scope.params[param]);
});
return displayName;
};
scope.name = getDisplayName();
}
};
});
@stevenpetryk
Copy link

Thank you for this!

Copy link

ghost commented Mar 14, 2014

I'm having trouble using it in my app. Can you write just a few steps please?

What do I have to write in the view?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment