I am trying to develop a directive that can be used in the following way:
<div before-today="old">{{exampleDate}}</div>
The purpose of my directive is to check if the date within the div is before "today" and if it is, apply the CSS class "old". However, I am facing an issue because the value of "exampleDate" is fetched through an AJAX call, so it is empty at link time. Do you have any suggestions on how to solve this? Here is what I have come up with so far (but it's not working as expected):
angular.module('myApp').directive('beforeToday', [
function () {
return {
restrict: 'A',
scope: {
},
link: function (scope, element, attr) {
var date = new Date(element.text());
var today = new Date();
if (today >= date) {
element.addClass(attr.beforeToday);
}
}
};
}
]);