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

Commit b24cc63

Browse files
committed
fix(ngSrc,ngHref): binding should set element prop as well as attr
IE9 ignores setAttribute('src', val) calls on img if "ng:src" attribute is present. It only fetches the image if element property is updated as well. Closes #935
1 parent 49dfdf8 commit b24cc63

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ng/directive/booleanAttrs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,16 @@ forEach(['src', 'href'], function(attrName) {
310310
attr.$$observers[attrName] = [];
311311
attr.$observe(normalized, function(value) {
312312
attr.$set(attrName, value);
313+
314+
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
315+
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
316+
// to set the property as well to achieve the desired effect
317+
if (msie) element.prop(attrName, value);
313318
});
314319
} else {
315320
// value present means that no interpolation, so copy to native attribute.
316321
attr.$set(attrName, value);
322+
element.prop(attrName, value);
317323
}
318324
};
319325
}

test/ng/directive/booleanAttrsSpec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ describe('boolean attr directives', function() {
8080
describe('ngSrc', function() {
8181

8282
it('should interpolate the expression and bind to src', inject(function($compile, $rootScope) {
83-
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope)
83+
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);
84+
8485
$rootScope.$digest();
8586
expect(element.attr('src')).toEqual('some/');
8687

@@ -91,6 +92,27 @@ describe('ngSrc', function() {
9192

9293
dealoc(element);
9394
}));
95+
96+
if (msie) {
97+
it('should update the element property as well as the attribute', inject(
98+
function($compile, $rootScope) {
99+
// on IE, if "ng:src" directive declaration is used and "src" attribute doesn't exist
100+
// then calling element.setAttribute('src', 'foo') doesn't do anything, so we need
101+
// to set the property as well to achieve the desired effect
102+
103+
var element = $compile('<div ng-src="some/{{id}}"></div>')($rootScope);
104+
105+
$rootScope.$digest();
106+
expect(element.prop('src')).toEqual('some/');
107+
108+
$rootScope.$apply(function() {
109+
$rootScope.id = 1;
110+
});
111+
expect(element.prop('src')).toEqual('some/1');
112+
113+
dealoc(element);
114+
}));
115+
}
94116
});
95117

96118

0 commit comments

Comments
 (0)