Reputation: 9194
I have an angularjs ng-class function I am trying to use to set my table row color. I am fairly new to both angular and js so my error is most likely user error. I am trying to check if a date field is null / undefined then trying to set the row color accordingly, but I am getting a parse error on the date. Do I need to do something different with my date?
Thanks!
html setting row color:
<tr data-ng-repeat="s in data.sites" data-ng-class="setRowConditional({{s.LastScanReceived}})">
AngularJS controller function:
//Set row color
$scope.setRowConditional = function (lastScanReceived) {
var recDate = Date.parse(lastScanReceived);
if (typeof recDate === 'undefined') {
return "danger";
}
else {
return "active";
};
}
Parse Error:
Error: [$parse:syntax] Syntax Error: Token 'T13' is unexpected, expecting [)] at column 29 of the expression [setRowConditional(2013-12-18T13:59:09.397)] starting at [T13:59:09.397)].
Upvotes: 2
Views: 4660
Reputation: 10430
If you setup a JSFiddle it might be helpul, but off hand I would say you probably don't need the curly braces in your function call. That means you end up with the following (modified) line of code:
<tr data-ng-repeat="s in data.sites" data-ng-class="setRowConditional(s.LastScanReceived)">
In general you need the curly braces when replacing text directly in your html template. Give this a try and let me know if it works!
Edit
By the way, there is another way to conditionally set the class of an element. You can basically use the following syntax:
<ANY ng-class="{'warning' : isWarningClass, 'error' : isErrorClass}"></ANY>
In this example, "isWarningClass" is a boolean on the scope which is set when you want to change the class of the "any" element to "warning". "isErrorClass" follows the same idea. The nice thing about this example is it shows you how to conditionally specify multiple classes on the same element. This might be a different (simpler?) way to get what you are looking for. Again, good luck!
Upvotes: 5