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

feat($compile): allow any attribute to use allOrNothing interpolation #8399

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
attrs[nName] = true; // presence means true
}
}
addAttrInterpolateDirective(node, directives, value, nName);
addAttrInterpolateDirective(node, directives, value, nName, isNgAttr);
addDirective(directives, nName, 'A', maxPriority, ignoreDirective, attrStartName,
attrEndName);
}
Expand Down Expand Up @@ -1929,7 +1929,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}


function addAttrInterpolateDirective(node, directives, value, name) {
function addAttrInterpolateDirective(node, directives, value, name, allOrNothing) {
var interpolateFn = $interpolate(value, true);

// no interpolation found -> ignore
Expand Down Expand Up @@ -1958,7 +1958,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
// we need to interpolate again, in case the attribute value has been updated
// (e.g. by another directive's compile function)
interpolateFn = $interpolate(attr[name], true, getTrustedContext(node, name),
ALL_OR_NOTHING_ATTRS[name]);
ALL_OR_NOTHING_ATTRS[name] || allOrNothing);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably pointless and pedantic but would it be slightly more performant to put the allOrNothing check first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this will make a measurable difference, but in some cases putting the boolean first would win (when ng-attr-* is used frequently), in other cases it would lose (when href/src are used frequently). It would depend on the app, I guess


// if attribute was updated so that there is no interpolation going on we don't want to
// register any observers
Expand Down
11 changes: 11 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5423,6 +5423,17 @@ describe('$compile', function() {
expect(element.attr('test')).toBe('Misko');
}));

it('should remove attribute if any bindings are undefined', inject(function($compile, $rootScope) {
element = $compile('<span ng-attr-test="{{name}}{{emphasis}}"></span>')($rootScope);
$rootScope.$digest();
expect(element.attr('test')).toBeUndefined();
$rootScope.name = 'caitp';
$rootScope.$digest();
expect(element.attr('test')).toBeUndefined();
$rootScope.emphasis = '!!!';
$rootScope.$digest();
expect(element.attr('test')).toBe('caitp!!!');
}));

describe('in directive', function() {
beforeEach(module(function() {
Expand Down