snippetjavascriptMinor
Is a filter a good way to accomplish a dynamic class in angular?
Viewed 0 times
wayangulargoodfilterdynamicclassaccomplish
Problem
I want to apply a different badge color with bootstrap depending on a number.
I wrote a filter to accomplish this...
And it's used with interpolation like this...
Is this a good way to accomplish this?
I wrote a filter to accomplish this...
angular.module('Demo')
.filter('reserveStatus', function() {
return function(num) {
var klass = "";
num = parseInt(num, 10);
if (num > 2 && num = 5 && num = 10) {
klass = "alert-danger";
}
return klass;
}
});And it's used with interpolation like this...
Is this a good way to accomplish this?
Solution
Fun question, I think this is okay.
I would name
I would name
klass -> class and I would consider building a map for the values:var classMap = [
{ from : 3 , to : 5 , class : 'alert-success' },
{ from : 5 , to : 10 , class : 'alert-warning' },
{ from : 10, to : Infinity, class : 'alert-danger' }
];Code Snippets
var classMap = [
{ from : 3 , to : 5 , class : 'alert-success' },
{ from : 5 , to : 10 , class : 'alert-warning' },
{ from : 10, to : Infinity, class : 'alert-danger' }
];Context
StackExchange Code Review Q#60380, answer score: 2
Revisions (0)
No revisions yet.