Skip to content

Commit

Permalink
fix(ngSrc, ngSrcset, ngHref): only interpolate if all expressions are…
Browse files Browse the repository at this point in the history
… defined

BREAKING CHANGE

If `bar` is `undefined`, before `<img src="foo/{{bar}}.jpg">` yields
`<img src="foo/.jpg">`. With this change, the binding will not set `src`.

If you want the old behavior, you can do this: `<img src="foo/{{bar || ''}}.jpg">`.

The same applies for `srcset` and `href`.

Closes angular#6984
  • Loading branch information
btford committed Apr 30, 2014
1 parent 438348a commit ae8d4d1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/ng/directive/booleanAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
propName = null;
}

attr.$setAllOrNothing(normalized);
attr.$observe(normalized, function(value) {
if (!value)
return;
Expand Down
7 changes: 3 additions & 4 deletions test/ng/directive/booleanAttrsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,8 @@ describe('ngSrc', function() {
describe('ngSrcset', function() {
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);

$rootScope.$digest();
expect(element.attr('srcset')).toEqual('some/ 2x');
expect(element.attr('srcset')).toBeUndefined();

$rootScope.$apply(function() {
$rootScope.id = 1;
Expand All @@ -227,7 +226,7 @@ describe('ngHref', function() {
it('should interpolate the expression and bind to href', inject(function($compile, $rootScope) {
element = $compile('<div ng-href="some/{{id}}"></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('href')).toEqual('some/');
expect(element.attr('href')).toBeUndefined();;

$rootScope.$apply(function() {
$rootScope.id = 1;
Expand Down Expand Up @@ -258,7 +257,7 @@ describe('ngHref', function() {
element = $compile('<svg><a ng-href="some/{{id}}"></a></svg>')($rootScope);
var child = element.children('a');
$rootScope.$digest();
expect(child.attr('xlink:href')).toEqual('some/');
expect(child.attr('xlink:href')).toBeUndefined();

$rootScope.$apply(function() {
$rootScope.id = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/ng/directive/ngSrcsetSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ describe('ngSrcset', function() {
$rootScope.image = {};
element = $compile('<img ng-srcset="{{image.url}} 2x">')($rootScope);
$rootScope.$digest();
expect(element.attr('srcset')).toEqual(' 2x');
expect(element.attr('srcset')).toBeUndefined();
}));
});

0 comments on commit ae8d4d1

Please sign in to comment.