Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert date without using scope or controller in angular

Tags:

angularjs

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.

like image 626
Frenz Avatar asked Jun 13 '26 21:06

Frenz


1 Answers

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

like image 127
Tiago Fabre Avatar answered Jun 17 '26 02:06

Tiago Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!