From a84694d8935008b57b7f5ce031a5ccf94f2c1615 Mon Sep 17 00:00:00 2001 From: Murray Smith Date: Fri, 4 Jul 2014 04:27:34 -0600 Subject: [PATCH] support setting url #fragments in state params --- src/state.js | 4 ++-- src/stateDirectives.js | 16 ++++++++++++---- src/urlRouter.js | 4 ++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/state.js b/src/state.js index b0822b7ac..8c0120b93 100644 --- a/src/state.js +++ b/src/state.js @@ -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) { @@ -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 }); }; diff --git a/src/stateDirectives.js b/src/stateDirectives.js index be0497d5f..040984ad3 100644 --- a/src/stateDirectives.js +++ b/src/stateDirectives.js @@ -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) { @@ -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); diff --git a/src/urlRouter.js b/src/urlRouter.js index e4a76524f..b9348d598 100644 --- a/src/urlRouter.js +++ b/src/urlRouter.js @@ -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['#']); }, /** @@ -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) {