Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use angular filter outside the angular application

I want to format dates on the client-side using angular date filter. I'd like to do that way, because I use angular in some places of my application and I'd like my dates to be formatted uniformly in the whole app.

What I'm trying to do is:

function formatDatetime(date, format) {
  var ngDateFilter = angular.getDateFilter(); //that's what I'm asking about
  return ngDateFilter(date, format);
}

I use angular applications only on several pages, but dates are spread over various pages (with and without angular app).

like image 376
mrzasa Avatar asked Oct 02 '13 10:10

mrzasa


1 Answers

OK, as usual I've found an answer. I did:

angular.injector(["ng"]).get("$filter")("date");

and it's OK.

EDIT

As meze observed, I could have also used:

angular.injector(["ng"]).get("dateFilter")

An example with number filter for currency:

var filter = angular.injector(["ng"]).get("$filter")("number");

$('#Price').val(filter(price));
like image 163
mrzasa Avatar answered Sep 22 '22 15:09

mrzasa