JyotiChhetri
JyotiChhetri

Reputation: 299

Directive to format date in angularjs?

I have a span in ng-repeat as follows:

span(humanize-date="{{file.date}}"

I'm to create a directive so that, directive changes the date format

directive('humanizeDate', [
  function() {
    return {
      restrict: 'EA',
      template: '<div value="{{formattedDate}}"/>',
      replace: true,
      scope: {
        formattedDate: '=humanizeDate'
      },
      link: function(scope, elem, attrs) {
        return scope.formatedDate = moment.duration(scope.humanizeDate).humanize();
      }
    };
  }
]);

Upvotes: 0

Views: 467

Answers (2)

alvarodoune
alvarodoune

Reputation: 931

just use:

<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>

here is more information about https://docs.angularjs.org/api/ng/filter/date

hope to help

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

You should not use {{}} while you are using = inside your directive which indicated two way binding.

It should be direct variable reference rather than {{}}

span(humanize-date="file.date"

OR

Other way would be you could use @ in your directive isolated scope as your are giving {{}} with expression to attribute which indicated one way binding.

scope: {
  formattedDate: '@humanizeDate'
},

Upvotes: 0

Related Questions