Skip to content

Commit 7c311bf

Browse files
committed
fix($compile): ignore optional =-bound properties with empty value
Previously, optional empty '='-bound expressions were ignored --- erroneously they stopped being ignored, and no tests were caused to fail for this reason. This change restores the original ignoring behaviour while still preventing other errors fixed by 8a1eb16 Closes angular#12144 Closes angular#12259
1 parent 69f1ebd commit 7c311bf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2588,9 +2588,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
25882588

25892589
case '=':
25902590
if (!hasOwnProperty.call(attrs, attrName)) {
2591-
if (optional) break;
25922591
attrs[attrName] = void 0;
25932592
}
2593+
if (optional && !attrs[attrName]) break;
25942594

25952595
parentGet = $parse(attrs[attrName]);
25962596
if (parentGet.literal) {

test/ng/compileSpec.js

+27
Original file line numberDiff line numberDiff line change
@@ -3618,6 +3618,33 @@ describe('$compile', function() {
36183618
});
36193619

36203620

3621+
it('should ignore optional "="-bound property if value is the emptry string', function() {
3622+
module(function($compileProvider) {
3623+
$compileProvider.directive('testDir', valueFn({
3624+
scope: {prop: '=?'},
3625+
controller: function($scope) {
3626+
$scope.prop = $scope.prop || 'default';
3627+
this.getProp = function() {
3628+
return $scope.prop;
3629+
};
3630+
},
3631+
controllerAs: 'ctrl',
3632+
template: '<p></p>'
3633+
}));
3634+
});
3635+
inject(function($compile, $rootScope) {
3636+
element = $compile('<div test-dir></div>')($rootScope);
3637+
var scope = element.isolateScope();
3638+
expect(scope.ctrl.getProp()).toBe('default');
3639+
$rootScope.$digest();
3640+
expect(scope.ctrl.getProp()).toBe('default');
3641+
scope.prop = 'foop';
3642+
$rootScope.$digest();
3643+
expect(scope.ctrl.getProp()).toBe('foop');
3644+
});
3645+
});
3646+
3647+
36213648
describe('bind-once', function() {
36223649

36233650
function countWatches(scope) {

0 commit comments

Comments
 (0)