Skip to content

Commit 9b1b5b3

Browse files
davidrehermhevery
authored andcommitted
fix(angular1_router): ngLink should not throw an error if routeParams are undefined (angular#8460)
If routeParams are undefined, no href attribute should be rendered. This matches the behaviour of ngHref.
1 parent 3857c82 commit 9b1b5b3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

modules/angular1_router/src/ng_outlet.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ function ngLinkDirective($rootRouter, $parse) {
213213
let link = attrs.ngLink || '';
214214

215215
function getLink(params) {
216+
if (!params) {
217+
return;
218+
}
219+
216220
navigationInstruction = router.generate(params);
217221

218222
scope.$watch(function() { return router.isRouteActive(navigationInstruction); },

modules/angular1_router/test/ng_link_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ describe('ngLink', function () {
140140
navigateTo('/');
141141
expect(elt.find('a').attr('class')).toBe('ng-link-active');
142142
});
143+
144+
it('should not add a href if link attributes are undefined', function () {
145+
setup({baseHref: baseHref, html5Mode: html5Mode, hashPrefix: hashPrefix});
146+
configureRouter([
147+
{ path: '/a', component: 'oneCmp' },
148+
{ path: '/b', component: 'twoCmp', name: 'Two' }
149+
]);
150+
151+
var elt = compile('<a ng-link="something.undefined">link</a> | outer { <div ng-outlet></div> }');
152+
navigateTo('/a');
153+
expect(elt.find('a').hasAttr('href')).toBeFalsy();
154+
});
143155
}
144156

145157
function registerComponent(name, template, controller) {

0 commit comments

Comments
 (0)