mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-09 15:24:43 +00:00
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.
This commit is contained in:
parent
a631ceb223
commit
fd3071843c
2 changed files with 20 additions and 0 deletions
|
|
@ -302,6 +302,9 @@ forEach(['src', 'href'], function(attrName) {
|
||||||
priority: 99, // it needs to run after the attributes are interpolated
|
priority: 99, // it needs to run after the attributes are interpolated
|
||||||
link: function(scope, element, attr) {
|
link: function(scope, element, attr) {
|
||||||
attr.$observe(normalized, function(value) {
|
attr.$observe(normalized, function(value) {
|
||||||
|
if (!value)
|
||||||
|
return;
|
||||||
|
|
||||||
attr.$set(attrName, value);
|
attr.$set(attrName, value);
|
||||||
|
|
||||||
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
|
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
|
||||||
|
|
|
||||||
17
test/ng/directive/ngSrcSpec.js
Normal file
17
test/ng/directive/ngSrcSpec.js
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
describe('ngSrc', function() {
|
||||||
|
var element;
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
dealoc(element);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not result empty string in img src', inject(function($rootScope, $compile) {
|
||||||
|
$rootScope.image = {};
|
||||||
|
element = $compile('<img ng-src="{{image.url}}">')($rootScope);
|
||||||
|
$rootScope.$digest();
|
||||||
|
expect(element.attr('src')).not.toBe('');
|
||||||
|
expect(element.attr('src')).toBe(undefined);
|
||||||
|
}));
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue