Why does ng-class replace existing classes?

1,751 views
Skip to first unread message

Johan Steenkamp

unread,
Apr 3, 2012, 7:53:44 PM4/3/12
to ang...@googlegroups.com
I could not find a thread about this.

Would it not make more sense to retain any existing classes when applying a class dynamically using ng-class?


Its common if using jQuery to add/remove classes rather than replace them all which makes sense given how classes are typically used - in combination and not individually like IDs.

Example: Tabbed forms, where I set the active tab using class="active". Say I want to alter the tab text to red if the form in the tab is invalid I could use class="valid-{{myForm.$valid}}" and CSS style .valid-false {color:red}

However the binding will remove .active

Interested in why all classes are replaced and/or if I missed a better solution.

Thanks

Peter Bacon Darwin

unread,
Apr 4, 2012, 2:17:36 AM4/4/12
to ang...@googlegroups.com

Ng-class does not remove static entries inthe attribute. If you want both active and error then just create a controller method that returns an array of class strings based on whether the tab is in error and/or active.

...from my mobile.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/ViCeQ87Sps8J.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Misko Hevery

unread,
Apr 4, 2012, 2:18:51 AM4/4/12
to ang...@googlegroups.com
ng-class appends classes, why do you think it replaces???

-- misko
Reply all
Reply to author
Forward
0 new messages