AngularJS snippets

AngularJS code snippets, serving as a quick reference

Filter date in controller

 $filter('date')(item.date, "dd/MM/yyyy");
 $filter('date')(item.date, "yyyy-MM-dd HH:mm");

https://docs.angularjs.org/api/ng/filter/date

format string can be composed of the following elements:

  • 'yyyy': 4 digit representation of year (e.g. AD 1 => 0001, AD 2010 => 2010)
  • 'yy': 2 digit representation of year, padded (00-99). (e.g. AD 2001 => 01, AD 2010 => 10)
  • 'MM': Month in year, padded (01-12)
  • 'M': Month in year (1-12)
  • 'dd': Day in month, padded (01-31)
  • 'd': Day in month (1-31)
  • 'EEEE': Day in Week,(Sunday-Saturday)
  • 'EEE': Day in Week, (Sun-Sat)
  • 'HH': Hour in day, padded (00-23)
  • 'hh': Hour in AM/PM, padded (01-12)
  • 'mm': Minute in hour, padded (00-59)
  • 'ss': Second in minute, padded (00-59)
  • 'sss': Millisecond in second, padded (000-999)
  • 'a': AM/PM marker
  • 'medium': equivalent to 'MMM d, y h:mm:ss a' for en_US locale (e.g. Sep 3, 2010 12:05:08 PM)
  • 'short': equivalent to 'M/d/yy h:mm a' for en_US locale (e.g. 9/3/10 12:05 PM)
  • 'fullDate': equivalent to 'EEEE, MMMM d, y' for en_US locale (e.g. Friday, September 3, 2010)
  • 'longDate': equivalent to 'MMMM d, y' for en_US locale (e.g. September 3, 2010)
  • 'mediumDate': equivalent to 'MMM d, y' for en_US locale (e.g. Sep 3, 2010)
  • 'shortDate': equivalent to 'M/d/yy' for en_US locale (e.g. 9/3/10)
  • 'mediumTime': equivalent to 'h:mm:ss a' for en_US locale (e.g. 12:05:08 PM)
  • 'shortTime': equivalent to 'h:mm a' for en_US locale (e.g. 12:05 PM)

Fake AJAX requests

function fakeAjax(returnValue, delay) {
  return $q(function(resolve, reject) {
    $timeout(function() {
      resolve(returnValue)
    }, delay)
  }
}

Filters with promises

https://egghead.io/lessons/angularjs-stateful-filters-with-promises-in-angularjs