Skip to content

Commit a602bb3

Browse files
committed
added absolute option to $state.href
1 parent b7b2ceb commit a602bb3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/state.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,24 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
411411
};
412412

413413
$state.href = function href(stateOrName, params, options) {
414-
options = extend({ lossy: true, inherit: false, relative: $state.$current }, options || {});
414+
options = extend({ lossy: true, inherit: false, absolute: false, relative: $state.$current }, options || {});
415415
var state = findState(stateOrName, options.relative);
416416
if (!isDefined(state)) return null;
417417

418418
params = inheritParams($stateParams, params || {}, $state.$current, state);
419419
var nav = (state && options.lossy) ? state.navigable : state;
420420
var url = (nav && nav.url) ? nav.url.format(normalize(state.params, params || {})) : null;
421-
return !$locationProvider.html5Mode() && url ? "#" + url : url;
421+
if (!$locationProvider.html5Mode() && url) {
422+
url = "#" + url;
423+
}
424+
if (options.absolute && url) {
425+
url = $location.protocol() + '://' +
426+
$location.host() +
427+
($location.port() == 80 || $location.port() == 443 ? '' : ':' + $location.port()) +
428+
(!$locationProvider.html5Mode() && url ? '/' : '') +
429+
url;
430+
}
431+
return url;
422432
};
423433

424434
$state.get = function (stateOrName) {

test/stateSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ describe('state', function () {
510510
expect($state.href("about.person", { person: "bob" })).toEqual("#/about/bob");
511511
expect($state.href("about.person.item", { person: "bob", id: null })).toEqual("#/about/bob/");
512512
}));
513+
514+
it('generates absolute url when absolute is true', inject(function ($state) {
515+
expect($state.href("about.sidebar", null, { absolute: true })).toEqual("http://server/#/about");
516+
locationProvider.html5Mode(true);
517+
expect($state.href("about.sidebar", null, { absolute: true })).toEqual("http://server/about");
518+
}));
513519
});
514520

515521
describe('.get()', function () {

0 commit comments

Comments
 (0)