Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 87a1dad

Browse files
author
Wei Wang
committed
feat(location): add ng-noserver attr to force html5mode link rewriting
See this discussion: #14959 When using html5mode, sometimes it is necessary to not use the <base> tag in order to support SVG icons. An example of this is in the discussion linked above. When we do this, sometimes we also want to disable "rewriteLinks" so that we can still leave the Angular application via navigation. This feature allows the user to explicitly mark an <a> tag as a link that should not refresh from server, even if rewriteLinks is disabled.
1 parent 494d12f commit 87a1dad

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/ng/location.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,9 @@ function $LocationProvider() {
749749
* true, and a base tag is not present, an error will be thrown when `$location` is injected.
750750
* See the {@link guide/$location $location guide for more information}
751751
* - **rewriteLinks** - `{boolean}` - (default: `true`) When html5Mode is enabled,
752-
* enables/disables url rewriting for relative links.
752+
* enables/disables url rewriting for relative links. Note that if this is disabled, you can
753+
* still force a link to be rewritten by adding the "ng-noserver" attr to the `<a>` element:
754+
* `<a href="somewhere" ng-noserver>click me</a>`.
753755
*
754756
* @returns {Object} html5Mode object if used as getter or itself (chaining) if used as setter
755757
*/
@@ -867,7 +869,7 @@ function $LocationProvider() {
867869
// TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
868870
// currently we open nice url link and redirect then
869871

870-
if (!html5Mode.rewriteLinks || event.ctrlKey || event.metaKey || event.shiftKey || event.which === 2 || event.button === 2) return;
872+
if (event.ctrlKey || event.metaKey || event.shiftKey || event.which === 2 || event.button === 2) return;
871873

872874
var elm = jqLite(event.target);
873875

@@ -877,6 +879,8 @@ function $LocationProvider() {
877879
if (elm[0] === $rootElement[0] || !(elm = elm.parent())[0]) return;
878880
}
879881

882+
if (!html5Mode.rewriteLinks && isUndefined(elm.attr('ng-noserver'))) return;
883+
880884
var absHref = elm.prop('href');
881885
// get the actual href attribute - see
882886
// http://msdn.microsoft.com/en-us/library/ie/dd347148(v=vs.85).aspx

test/ng/locationSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,25 @@ describe('$location', function() {
16011601
});
16021602

16031603

1604+
it('should rewrite links with the "ng-noserver" attr, even with rewriteLinks disabled', function() {
1605+
configureTestLink({
1606+
linkHref: 'link?a#b',
1607+
attrs: 'ng-noserver',
1608+
html5Mode: {enabled: true, rewriteLinks:false},
1609+
supportHist: true
1610+
});
1611+
initService({html5Mode:{enabled: true, rewriteLinks:false},supportHistory:true});
1612+
inject(
1613+
initBrowser({ url: 'http://host.com/base/index.html', basePath: '/base/index.html' }),
1614+
setupRewriteChecks(),
1615+
function($browser) {
1616+
browserTrigger(link, 'click');
1617+
expectRewriteTo($browser, 'http://host.com/base/link?a#b');
1618+
}
1619+
);
1620+
});
1621+
1622+
16041623
it('should rewrite full url links to same domain and base path', function() {
16051624
configureTestLink({linkHref: 'http://host.com/base/new'});
16061625
initService({html5Mode:true,supportHistory:false,hashPrefix:'!'});

0 commit comments

Comments
 (0)