-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Fix ngClass mixed array/object when object instance does not changes #14405
Conversation
As I commented in #14394 there is this case that are not resolved here and it has two solutions. The problem: // this test actually passes
it('should support mixed nested array and object leafs variable', inject(function($rootScope, $compile) {
$rootScope.classVar = ['red', ['green', {blue: true}]];
element = $compile('<div class="existing" ng-class="classVar"></div>')($rootScope);
$rootScope.$digest();
expect(element.hasClass('red')).toBeTruthy();
expect(element.hasClass('green')).toBeTruthy();
expect(element.hasClass('blue')).toBeTruthy();
}));
// this test fails even with this patch
it('should support mixed nested array and object leafs variable with mutating object', inject(function($rootScope, $compile) {
$rootScope.classVar = ['red', ['green', {blue: true}]];
element = $compile('<div class="existing" ng-class="classVar"></div>')($rootScope);
$rootScope.$digest();
$rootScope.classVar[1][1].blue = false;
expect(element.hasClass('red')).toBeTruthy();
expect(element.hasClass('green')).toBeTruthy();
expect(element.hasClass('blue')).toBeFalsy();
})); Solutions:
¿Which do you consider more acceptable? |
The docs are very explicit about what types of expressions are supported in According to the docs, the expression used with
(Where objects are treated as key-value pairs, with keys denoting classes and values determining the presense or absense of the corresponding key/class.) |
@@ -409,6 +409,18 @@ describe('ngClass', function() { | |||
expect(e2.hasClass('even')).toBeTruthy(); | |||
expect(e2.hasClass('odd')).toBeFalsy(); | |||
})); | |||
|
|||
it('should support changing multiple classes via variable array mixed with conditionally via a map', inject(function($rootScope, $compile) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pleasd wrap lines at 100 chars.
I left a couple of minor comments wrt the test, but LGTM otherwise. 👍 |
727ab82
to
3b91bb1
Compare
@gkalpak I think that is now right. |
It's OK, I'll squash while merging. Thx 👍 |
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...) bugfix
What is the current behavior? (You can also link to an open issue here): when the expression is [{}] it does not tracks changes inside the object
What is the new behavior (if this is a feature change)?: now it tracks changes correctly
Does this PR introduce a breaking change?: nop
Please check if the PR fulfills these requirements
Other information:
In this plnkr you can see the failure:
#14404 also fixes this problem by doing a refactor. (it tries to be a performance optimization)
This PR can be combined into #14394 if you ask.