The only significant difference I can think of is around the data-binding
1st approach, using filter
<td class="{{ user | classify }}">
Pro: leverage angular's powerful filter mechanism, shorter and nicer syntax
Con: a $watch
will be put on the user
object to watch for changes, and then call the filter
, which actually loop through $filterProvider
to find the correct provider
function and execute it (I guess you'll figure out the overhead implication of this process)
2nd approach, using object's property
<td class="{{ user.cssClass }}"><!-- or --><td ng-class="user.cssClass">
Pro: just a normal 2-way binding (the magic of Angular JS), and you are doing it in the Angular-way
Con: not really a "con", this approach leverage automatic 2-way binding, which is a $watch
on the user object's property to watch for changes, and update the class attribute
3rd approach
<td ng-class="computeCssClass(user)">
Pro: You have more control of when the class attribute got updated (by the return value of the function)
Con: it's easier to mess things up. [Edited] Overhead of $watch
a function: the function is executed multiple times in a $digest
and its return value got comparing to watch for changes, can be expensive.
If performance is what you care about most, the 3rd approach would be a good choice for you.
If you want to use ng-class
, of all the above approaches, the 2nd approach will have the least overhead.
You can consider one-time binding, or a custom directive to handle updating class attributes. If you use the "class" attribute, you will be able to leverage the one-time binding syntax:
<td class="{{ ::user.cssClass }}">
Pre-compiling some presentation attributes from the data is always good, unless you have very specific requirement such as changing the user's dashboard background colour/image basing on the current weather of his/her location (well, even with this requirement, the weather is what should be $watch
for)
I saw that you compute the class
on a <td>
. It's very easy to reach to the number of $watch
that slows the page down significantly with table (read the 2nd answer of the referenced question above)
Anyway, it's all up to you which approach should you use, basing on the real-world context of the application you are developing.