Skip to content

feat(ui-sref): support URL fragments in html5Mode #1455

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
7 changes: 4 additions & 3 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,10 +1112,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* - **`relative`** - {object=$state.$current}, When transitioning with relative path (e.g '^'),
* defines which state to be relative from.
* - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
*
* @param {string} fragment (optional) The URL fragment to append (only for HTML5Mode).
*
* @returns {string} compiled state url
*/
$state.href = function href(stateOrName, params, options) {
$state.href = function href(stateOrName, params, options, fragment) {
options = extend({
lossy: true,
inherit: true,
Expand All @@ -1135,7 +1136,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
}
return $urlRouter.href(nav.url, filterByKeys(objectKeys(state.params), params || {}), {
absolute: options.absolute
});
}, fragment);
};

/**
Expand Down
8 changes: 4 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 < 2) throw new Error("Invalid state ref '" + ref + "'");
return { state: parsed[1], paramExpr: parsed[2] || null, fragment: parsed[3] };
}

function stateContext(el) {
Expand Down Expand Up @@ -103,7 +103,7 @@ function $StateRefDirective($state, $timeout) {
if (newVal) params = angular.copy(newVal);
if (!nav) return;

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

var activeDirective = uiSrefActive[1] || uiSrefActive[0];
if (activeDirective) {
Expand Down
6 changes: 5 additions & 1 deletion src/urlRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,11 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
* @param {object=} options Options object. The options are:
*
* - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
* @param {string} fragment (optional) The URL fragment to append (only for HTML5Mode).
*
* @returns {string} Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher`
*/
href: function(urlMatcher, params, options) {
href: function(urlMatcher, params, options, fragment) {
if (!urlMatcher.validates(params)) return null;

var isHtml5 = $locationProvider.html5Mode();
Expand All @@ -391,6 +392,9 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
if (!isHtml5 && url !== null) {
url = "#" + $locationProvider.hashPrefix() + url;
}
else if (isHtml5 && url !== null && fragment !== undefined) {
url += '#' + fragment;
}
url = appendBasePath(url, isHtml5, options.absolute);

if (!options.absolute || !url) {
Expand Down
6 changes: 6 additions & 0 deletions test/stateDirectivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ describe('uiStateRef', function() {
expect($state.current.name).toEqual('top');
expect($stateParams).toEqualData({});
}));

it('should support URL fragments', inject(function ($rootScope, $compile) {
var el = angular.element('<a ui-sref="contacts.item.detail({id: 1})#contact-info"></a>');
$compile(el)($rootScope);
expect(el.attr('href')).toBe('/contacts/1#contact-info');
}));
});

describe('forms', function() {
Expand Down
7 changes: 7 additions & 0 deletions test/urlRouterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ describe("UrlRouter", function () {

expect($urlRouter.href(new UrlMatcher('/hello'))).toBe('#/hello');
}));

it('should support fragments in html5Mode', inject(function($urlRouter, $urlMatcherFactory) {
$lp.html5Mode(true);

var matcher = new UrlMatcher("/foo#item-:id");
expect($urlRouter.href(matcher, {id: 1})).toBe('/foo#item-1');
}));
});
});

Expand Down