Skip to content

support setting url #fragments in state params #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
}

// Filter parameters before we pass them to event handlers etc.
toParams = filterByKeys(objectKeys(to.params), toParams || {});
toParams = filterByKeys(objectKeys(to.params).concat('#'), toParams || {});

// Broadcast start event and cancel the transition if requested
if (options.notify) {
Expand Down Expand Up @@ -1116,7 +1116,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
if (!nav || !nav.url) {
return null;
}
return $urlRouter.href(nav.url, filterByKeys(objectKeys(state.params), params || {}), {
return $urlRouter.href(nav.url, filterByKeys(objectKeys(state.params).concat('#'), params || {}), {
absolute: options.absolute
});
};
Expand Down
16 changes: 12 additions & 4 deletions src/stateDirectives.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function parseStateRef(ref, current) {
var preparsed = ref.match(/^\s*({[^}]*})\s*$/), parsed;
if (preparsed) ref = current + '(' + preparsed[1] + ')';
parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?$/);
if (!parsed || parsed.length !== 4) throw new Error("Invalid state ref '" + ref + "'");
return { state: parsed[1], paramExpr: parsed[3] || null };
parsed = ref.replace(/\n/g, " ").match(/^([^(]+?)\s*(\((.*)\))?(#(.*?))?$/);
if (!parsed || parsed.length < 4) throw new Error("Invalid state ref '" + ref + "'");
return { state: parsed[1], paramExpr: parsed[3] || null, '#': parsed[5] };
}

function stateContext(el) {
Expand Down Expand Up @@ -102,8 +102,16 @@ function $StateRefDirective($state, $timeout) {
if (newVal) params = newVal;
if (!nav) return;

var newHref = $state.href(ref.state, params, options);
if (ref['#']) {
if (angular.isObject(params)) {
params['#'] = ref['#'];
} else {
params = { '#': ref['#'] };
}
}

var newHref = $state.href(ref.state, params, options);

var activeDirective = uiSrefActive[1] || uiSrefActive[0];
if (activeDirective) {
activeDirective.$$setStateInfo(ref.state, params);
Expand Down
4 changes: 4 additions & 0 deletions src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
push: function(urlMatcher, params, options) {
$location.url(urlMatcher.format(params || {}));
if (options && options.replace) $location.replace();
if (params && params['#']) $location.hash(params['#']);
},

/**
Expand Down Expand Up @@ -387,6 +388,9 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
if (!isHtml5 && url) {
url = "#" + $locationProvider.hashPrefix() + url;
}
else if (isHtml5 && params['#']) {
url += '#' + params['#'];
}
url = appendBasePath(url, isHtml5, options.absolute);

if (!options.absolute || !url) {
Expand Down