Skip to content

Instantly share code, notes, and snippets.

@dnozay
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnozay/ed99f139cd88119979be to your computer and use it in GitHub Desktop.
Save dnozay/ed99f139cd88119979be to your computer and use it in GitHub Desktop.
angular momentjs directive.

If we detect angular is present, we can expose a directive:

<ANY moment date="scope.myDate" format="'lll'"></ANY>
'use strict';

/**
 * @ngdoc directive
 * @name momentjs.directive:moment
 * @description
 * # moment
 * <ANY moment date="scope.iso8601date" format="'lll'"></ANY>
 */
angular.module('momentjs')
  .directive('moment', function () {
    return {
      restrict: 'A',
      scope: {
        date: '=',
        format: '='
      },
      link: function postLink(scope, element, attrs) {
        if (scope.date) {
          scope.formattedDate = moment(scope.date).format(scope.format);
          element.text(scope.formattedDate);
        }
      }
    };
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment