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

Commit 469ea33

Browse files
shahatabtford
authored andcommitted
fix(ngHref): remove attribute when empty value instead of ignoring
Closes #2755
1 parent 1a2ed70 commit 469ea33

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/ng/directive/attrs.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,12 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
403403
}
404404

405405
attr.$observe(normalized, function(value) {
406-
if (!value)
407-
return;
406+
if (!value) {
407+
if (attrName === 'href') {
408+
attr.$set(name, null);
409+
}
410+
return;
411+
}
408412

409413
attr.$set(name, value);
410414

test/ng/directive/booleanAttrsSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,23 @@ describe('ngHref', function() {
252252
expect(element.attr('href')).toEqual('http://server');
253253
}));
254254

255+
it('should not set the href if ng-href is empty', inject(function($rootScope, $compile) {
256+
$rootScope.url = null;
257+
element = $compile('<a ng-href="{{url}}">')($rootScope);
258+
$rootScope.$digest();
259+
expect(element.attr('href')).toEqual(undefined);
260+
}));
261+
262+
it('should remove the href if ng-href changes to empty', inject(function($rootScope, $compile) {
263+
$rootScope.url = 'http://www.google.com/';
264+
element = $compile('<a ng-href="{{url}}">')($rootScope);
265+
$rootScope.$digest();
266+
267+
$rootScope.url = null;
268+
$rootScope.$digest();
269+
expect(element.attr('href')).toEqual(undefined);
270+
}));
271+
255272
if (isDefined(window.SVGElement)) {
256273
describe('SVGAElement', function() {
257274
it('should interpolate the expression and bind to xlink:href', inject(function($compile, $rootScope) {

0 commit comments

Comments
 (0)