Skip to content

Commit 4c1eb80

Browse files
committed
fix($compile): set attribute value even if ngAttr* contains no interpolation
Previoulsy, when the value of an `ngAttrXyz` attribute did not contain any interpolation, then the `xyz` attribute was never set. BTW, this commit adds a negligible overhead (since we have to set up a one-time watcher for example), but it is justifiable for someone that is using `ngAttrXyz` (instead of `xyz` directly). Fixes angular#15133
1 parent c2a3e2c commit 4c1eb80

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/ng/compile.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3251,16 +3251,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
32513251
}
32523252

32533253

3254-
function addAttrInterpolateDirective(node, directives, value, name, allOrNothing) {
3254+
function addAttrInterpolateDirective(node, directives, value, name, isNgAttr) {
32553255
var trustedContext = getTrustedContext(node, name);
3256-
allOrNothing = ALL_OR_NOTHING_ATTRS[name] || allOrNothing;
3256+
var mustHaveExpression = !isNgAttr;
3257+
var allOrNothing = ALL_OR_NOTHING_ATTRS[name] || isNgAttr;
32573258

3258-
var interpolateFn = $interpolate(value, true, trustedContext, allOrNothing);
3259+
var interpolateFn = $interpolate(value, mustHaveExpression, trustedContext, allOrNothing);
32593260

32603261
// no interpolation found -> ignore
32613262
if (!interpolateFn) return;
32623263

3263-
32643264
if (name === 'multiple' && nodeName_(node) === 'select') {
32653265
throw $compileMinErr('selmulti',
32663266
'Binding to the \'multiple\' attribute is not supported. Element: {0}',

test/ng/compileSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -11185,6 +11185,14 @@ describe('$compile', function() {
1118511185
expect(element.attr('test')).toBe('Misko');
1118611186
}));
1118711187

11188+
it('should set the attribute (after digest) even if there is no interpolation', inject(function() {
11189+
element = $compile('<span ng-attr-test="foo"></span>')($rootScope);
11190+
expect(element.attr('test')).toBeUndefined();
11191+
11192+
$rootScope.$digest();
11193+
expect(element.attr('test')).toBe('foo');
11194+
}));
11195+
1118811196
it('should remove attribute if any bindings are undefined', inject(function() {
1118911197
element = $compile('<span ng-attr-test="{{name}}{{emphasis}}"></span>')($rootScope);
1119011198
$rootScope.$digest();

0 commit comments

Comments
 (0)