Skip to content

Commit

Permalink
fix(directives): make ng:class-even/odd work with ng:class
Browse files Browse the repository at this point in the history
  • Loading branch information
Di Peng authored and IgorMinar committed Aug 19, 2011
1 parent 5da885c commit db6e4c4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,14 @@ function ngClass(selector) {
var existing = element[0].className + ' ';
return function(element){
this.$onEval(function(){
if (selector(this.$index)) {
var value = this.$eval(expression);
var scope = this;

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);
}
}, element);
Expand Down
44 changes: 44 additions & 0 deletions test/directivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,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.$eval();
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.$eval();
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

0 comments on commit db6e4c4

Please sign in to comment.