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

fix(ngClass): do not break on invalid values #16699

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
4 changes: 4 additions & 0 deletions src/ng/directive/ngClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ function classDirective(name, selector) {
}

function toClassString(classValue) {
if (!classValue) return classValue;

var classString = classValue;

if (isArray(classValue)) {
Expand All @@ -133,6 +135,8 @@ function classDirective(name, selector) {
classString = Object.keys(classValue).
filter(function(key) { return classValue[key]; }).
join(' ');
} else if (!isString(classValue)) {
classString = classValue + '';
}

return classString;
Expand Down
6 changes: 6 additions & 0 deletions test/ng/directive/ngClassSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ describe('ngClass', function() {
expect(element.hasClass('AnotB')).toBeFalsy();
}));

it('should not break when passed non-string/array/object, truthy values', inject(function($rootScope, $compile) {
element = $compile('<div ng-class="42"></div>')($rootScope);
$rootScope.$digest();
expect(element.hasClass('42')).toBeTruthy();
}));

it('should support adding multiple classes via an array mixed with conditionally via a map', inject(function($rootScope, $compile) {
element = $compile('<div class="existing" ng-class="[\'A\', {\'B\': condition}]"></div>')($rootScope);
$rootScope.$digest();
Expand Down