Last active
December 31, 2015 13:48
-
-
Save justinwinslow/7994944 to your computer and use it in GitHub Desktop.
Angular ui-router breadcrumbs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
}; | |
}); |
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
Thank you for this!