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())
       }

    })

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

@blue-sky it was a typo , $filter('date') not []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.