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

fix(directives): make ng:class-even/odd work with ng:class #510

Closed
wants to merge 2 commits 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
3 changes: 3 additions & 0 deletions src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,11 @@ function ngClass(selector) {
return function(element) {
this.$observe(function(scope) {
if (selector(scope.$index)) {
var ngClassVal = scope.$eval(element.attr('ng:class'));
if (isArray(ngClassVal)) ngClassVal = ngClassVal.join(' ');
var value = scope.$eval(expression);
if (isArray(value)) value = value.join(' ');
if (ngClassVal && ngClassVal !== value) value = value + ' ' + ngClassVal;
element[0].className = trim(existing + value);
}
});
Expand Down
44 changes: 44 additions & 0 deletions test/directivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,50 @@ describe("directive", function(){
expect(e2.hasClass('even')).toBeTruthy();
});


it('should allow both ng:class and ng:class-odd/even on the same element', function() {
var scope = compile('<ul>' +
'<li ng:repeat="i in [0,1]" ng:class="\'plainClass\'" ' +
'ng:class-odd="\'odd\'" ng:class-even="\'even\'"></li>' +
'<ul>');
scope.$flush();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);

expect(e1.hasClass('plainClass')).toBeTruthy();
expect(e1.hasClass('odd')).toBeTruthy();
expect(e1.hasClass('even')).toBeFalsy();
expect(e2.hasClass('plainClass')).toBeTruthy();
expect(e2.hasClass('even')).toBeTruthy();
expect(e2.hasClass('odd')).toBeFalsy();
});


it('should allow both ng:class and ng:class-odd/even with multiple classes', function() {
var scope = compile('<ul>' +
'<li ng:repeat="i in [0,1]" ng:class="[\'A\', \'B\']" ' +
'ng:class-odd="[\'C\', \'D\']" ng:class-even="[\'E\', \'F\']"></li>' +
'<ul>');
scope.$flush();
var e1 = jqLite(element[0].childNodes[1]);
var e2 = jqLite(element[0].childNodes[2]);

expect(e1.hasClass('A')).toBeTruthy();
expect(e1.hasClass('B')).toBeTruthy();
expect(e1.hasClass('C')).toBeTruthy();
expect(e1.hasClass('D')).toBeTruthy();
expect(e1.hasClass('E')).toBeFalsy();
expect(e1.hasClass('F')).toBeFalsy();

expect(e2.hasClass('A')).toBeTruthy();
expect(e2.hasClass('B')).toBeTruthy();
expect(e2.hasClass('E')).toBeTruthy();
expect(e2.hasClass('F')).toBeTruthy();
expect(e2.hasClass('C')).toBeFalsy();
expect(e2.hasClass('D')).toBeFalsy();
});


describe('ng:style', function(){
it('should set', function(){
var scope = compile('<div ng:style="{height: \'40px\'}"></div>');
Expand Down