Skip to content

Commit

Permalink
fix(ngSrc, ngSrcset): only interpolate if all expressions are defined
Browse files Browse the repository at this point in the history
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` as well.

Closes angular#6984
  • Loading branch information
btford committed May 2, 2014
1 parent 1dd67f2 commit 003b5ad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1854,9 +1854,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
"ng- versions (such as ng-click instead of onclick) instead.");
}

var allOrNothingAttrs = [
'ngSrc',
'ngSrcset',
'src',
'srcset'
];

// we need to interpolate again, in case the attribute value has been updated
// (e.g. by another directive's compile function)
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name));
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name),
allOrNothingAttrs.indexOf(name) > -1);

// if attribute was updated so that there is no interpolation going on we don't want to
// register any observers
Expand Down
2 changes: 1 addition & 1 deletion test/ng/directive/booleanAttrsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('ngSrcset', function() {
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 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 003b5ad

Please sign in to comment.