I'm trying to figure out how can I insert a date in my html without using a scope or controller. I tried something like this but it doesn't seem to work.
<p>Sample text {{Date.now().toString() | date:'yyyy'}} sample text</p>
I also tried without .toString but doesnt work as well
Would be great if you could let me know how I can achieve this.
Thanks in advance.
To solve that without a controller you can use a custom directive where you can just add a kind of div in the HTML and it will render the date.
with that you can do something like that:
HTML
<span date-now="MM/dd/yyyy"></span>
JS
.directive('dateNow', ['$filter', function($filter) {
return {
link: function( $scope, $element, $attrs) {
$element.text($filter('date')(new Date(), $attrs.dateNow));
}
};
}])
Result:
05/10/2016
Custom directives - Angular reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With