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

Commit f3a5658

Browse files
committedSep 20, 2018
fix(ngClass): do not break on invalid values
Previously, when an `ngClass` expression evaluated to something that was not a string, array or object (and was truthy), an error would be thrown while trying to call `.split()` on a non-string value. This error was not very helpful for the user to identify the root cause of the problem. This commit fixes it by ensuring such values are converted to string. Fixes #16697 Closes #16699
1 parent 582b03b commit f3a5658

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed
 

‎src/ng/directive/ngClass.js

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ function classDirective(name, selector) {
125125
}
126126

127127
function toClassString(classValue) {
128+
if (!classValue) return classValue;
129+
128130
var classString = classValue;
129131

130132
if (isArray(classValue)) {
@@ -133,6 +135,8 @@ function classDirective(name, selector) {
133135
classString = Object.keys(classValue).
134136
filter(function(key) { return classValue[key]; }).
135137
join(' ');
138+
} else if (!isString(classValue)) {
139+
classString = classValue + '';
136140
}
137141

138142
return classString;

‎test/ng/directive/ngClassSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ describe('ngClass', function() {
8888
expect(element.hasClass('AnotB')).toBeFalsy();
8989
}));
9090

91+
it('should not break when passed non-string/array/object, truthy values', inject(function($rootScope, $compile) {
92+
element = $compile('<div ng-class="42"></div>')($rootScope);
93+
$rootScope.$digest();
94+
expect(element.hasClass('42')).toBeTruthy();
95+
}));
96+
9197
it('should support adding multiple classes via an array mixed with conditionally via a map', inject(function($rootScope, $compile) {
9298
element = $compile('<div class="existing" ng-class="[\'A\', {\'B\': condition}]"></div>')($rootScope);
9399
$rootScope.$digest();

0 commit comments

Comments
 (0)
This repository has been archived.