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

Commit b6e4a71

Browse files
xrchenmhevery
authored andcommitted
fix(ngSrc): don't set src if value is empty string
Current implementation of ngSrc may lead to empty src attribute when page is loading. For example: <img ng-src="{{image.url}}"> can be temporarily rendered as <img src=""> before the image resource is loaded. Some browser emits a request to the current page when seeing <img src=""> (Firefox13 and IE8 will, Chromium20 won't), which leads to performance problems.
1 parent d9eff86 commit b6e4a71

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/ng/directive/booleanAttrs.js

+3
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ forEach(['src', 'href'], function(attrName) {
302302
priority: 99, // it needs to run after the attributes are interpolated
303303
link: function(scope, element, attr) {
304304
attr.$observe(normalized, function(value) {
305+
if (!value)
306+
return;
307+
305308
attr.$set(attrName, value);
306309

307310
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist

test/ng/directive/ngSrcSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
describe('ngSrc', function() {
4+
var element;
5+
6+
afterEach(function() {
7+
dealoc(element);
8+
});
9+
10+
it('should not result empty string in img src', inject(function($rootScope, $compile) {
11+
$rootScope.image = {};
12+
element = $compile('<img ng-src="{{image.url}}">')($rootScope);
13+
$rootScope.$digest();
14+
expect(element.attr('src')).not.toBe('');
15+
expect(element.attr('src')).toBe(undefined);
16+
}));
17+
});

0 commit comments

Comments
 (0)