Skip to content

Commit cbf9e74

Browse files
committed
fix(ngSrc, ngSrcset, ngHref): only interpolate if all expressions are 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
1 parent 81b919d commit cbf9e74

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

Diff for: src/ng/directive/booleanAttrs.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ forEach(BOOLEAN_ATTR, function(propName, attrName) {
365365
// ng-src, ng-srcset, ng-href are interpolated
366366
forEach(['src', 'srcset', 'href'], function(attrName) {
367367
var normalized = directiveNormalize('ng-' + attrName);
368-
ngAttributeAliasDirectives[normalized] = function() {
368+
ngAttributeAliasDirectives[normalized] = ['$interpolate', function($interpolate) {
369369
return {
370370
priority: 99, // it needs to run after the attributes are interpolated
371371
link: function(scope, element, attr) {
@@ -379,6 +379,7 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
379379
propName = null;
380380
}
381381

382+
attr.$setAllOrNothing(normalized);
382383
attr.$observe(normalized, function(value) {
383384
if (!value)
384385
return;
@@ -393,5 +394,5 @@ forEach(['src', 'srcset', 'href'], function(attrName) {
393394
});
394395
}
395396
};
396-
};
397+
}];
397398
});

Diff for: test/ng/directive/booleanAttrsSpec.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,8 @@ describe('ngSrc', function() {
202202
describe('ngSrcset', function() {
203203
it('should interpolate the expression and bind to srcset', inject(function($compile, $rootScope) {
204204
var element = $compile('<div ng-srcset="some/{{id}} 2x"></div>')($rootScope);
205-
206205
$rootScope.$digest();
207-
expect(element.attr('srcset')).toEqual('some/ 2x');
206+
expect(element.attr('srcset')).toBeUndefined();
208207

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

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

263262
$rootScope.$apply(function() {
264263
$rootScope.id = 1;

Diff for: test/ng/directive/ngSrcsetSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ describe('ngSrcset', function() {
1111
$rootScope.image = {};
1212
element = $compile('<img ng-srcset="{{image.url}} 2x">')($rootScope);
1313
$rootScope.$digest();
14-
expect(element.attr('srcset')).toEqual(' 2x');
14+
expect(element.attr('srcset')).toBeUndefined();
1515
}));
1616
});

0 commit comments

Comments
 (0)