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

Commit 772440c

Browse files
jbedardJason Bedard
authored andcommitted
fix($compile): fix ng-prop-* with undefined values
Fixes #16797 Closes #16798
1 parent 1fbddf9 commit 772440c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/ng/compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3837,7 +3837,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
38373837
pre: function ngPropPreLinkFn(scope, $element) {
38383838
function applyPropValue() {
38393839
var propValue = ngPropGetter(scope);
3840-
$element.prop(propName, sanitizer(propValue));
3840+
$element[0][propName] = sanitizer(propValue);
38413841
}
38423842

38433843
applyPropValue();

test/ng/ngPropSpec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,48 @@ describe('ngProp*', function() {
4949
expect(element.prop('asdf')).toBe(true);
5050
}));
5151

52+
// https://github.com/angular/angular.js/issues/16797
53+
it('should support falsy property values', inject(function($rootScope, $compile) {
54+
var element = $compile('<span ng-prop-text="myText" />')($rootScope);
55+
// Initialize to truthy value
56+
$rootScope.myText = 'abc';
57+
$rootScope.$digest();
58+
expect(element.prop('text')).toBe('abc');
59+
60+
// Assert various falsey values get assigned to the property
61+
$rootScope.myText = '';
62+
$rootScope.$digest();
63+
expect(element.prop('text')).toBe('');
64+
$rootScope.myText = 0;
65+
$rootScope.$digest();
66+
expect(element.prop('text')).toBe(0);
67+
$rootScope.myText = false;
68+
$rootScope.$digest();
69+
expect(element.prop('text')).toBe(false);
70+
$rootScope.myText = undefined;
71+
$rootScope.$digest();
72+
expect(element.prop('text')).toBeUndefined();
73+
$rootScope.myText = null;
74+
$rootScope.$digest();
75+
expect(element.prop('text')).toBe(null);
76+
}));
77+
78+
it('should directly map special properties (class)', inject(function($rootScope, $compile) {
79+
var element = $compile('<span ng-prop-class="myText" />')($rootScope);
80+
$rootScope.myText = 'abc';
81+
$rootScope.$digest();
82+
expect(element[0].class).toBe('abc');
83+
expect(element).not.toHaveClass('abc');
84+
}));
85+
86+
it('should not use jQuery .prop() to avoid jQuery propFix/hooks', inject(function($rootScope, $compile) {
87+
var element = $compile('<span ng-prop-class="myText" />')($rootScope);
88+
spyOn(jqLite.prototype, 'prop');
89+
$rootScope.myText = 'abc';
90+
$rootScope.$digest();
91+
expect(jqLite.prototype.prop).not.toHaveBeenCalled();
92+
}));
93+
5294
it('should support mixed case using underscore-separated names', inject(function($rootScope, $compile) {
5395
var element = $compile('<span ng-prop-a_bcd_e="value" />')($rootScope);
5496
$rootScope.value = 123;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy