2

Access the shared date format :

{{ dateValue | date:{{dateFormat}} }}

The service :

app.service('formatting', function() {
      var format = new Object();

      format.dateFormat = 'medium'

      var getDateFormat = function(){
          return format.dateFormat;
      };

      return {
        getDateFormat : getDateFormat
      };

    });

But I don't think access the date format in this way is legal ?

How to centralize the the date format so can be used from multiple view pages ?

Update :

Here is fiddle I'm trying :

http://jsfiddle.net/DG24c/99/

fiddle src :

html : 
<div ng-app="myApp">
  <div ng-controller="farmController">
      <div>{{ cTime | dateFormat }}
    </div>
  </div>
</div>


javascript : 

var myApp = angular.module('myApp', []);

myApp.controller("farmController",function($scope){
    $scope.cTime = 1439396762286;   
})

myApp.service('formatting', function() {

      var getDateFormat = function(){
          return 'medium'
      };

      return {
        getDateFormat : getDateFormat
      };

    });


myApp.filter('dateFormat', function($filter, formatting) {

       return function(date) {
         return $filter['date'](date, formatting.getDateFormat())
       }

    })
blue-sky
  • 51,962
  • 152
  • 427
  • 752

1 Answers1

3

Create new filter myDateFormat that will use your service and dateFilter. (Check AngularJS - convert dates in controller for example of how to use date filter in controller or filter or service etc)

You will use it as

 {{ dateValue | myDateFormat }}

And example filter would be:

app.filter('myDateFormat', function($filter, formatting) {

   return function(date) {
     return $filter('date')(date, formatting.getDateFormat())
   }

})

JSFIDDLE: http://jsfiddle.net/pwhnaxd7/

Community
  • 1
  • 1
vittore
  • 17,449
  • 6
  • 44
  • 82