So when browsing the angularJS docs I notice that various filter templates are like this:
{{ date_expression | date : date : format}}
What is the meaning of date : date?
If I run the following code: (JS Fiddle http://jsfiddle.net/6Jt4h/2/)
HTML:
<div ng-app="testApp" ng-controller="testCon">
{{dt1 | date : 'MM/dd/yyyy @ h:mma'}}<br>
{{dt1 | date : dt1 : 'MM/dd/yyyy @ h:mma'}}
</div>
Javascript:
var testApp = angular.module('testApp', []);
testApp.controller('testCon', function($scope) {
$scope.dt1 = new Date();
});
I get the following output:
03/20/2014 @ 3:19PM
T3u 3PMr 20 2014 15:19:46 G3T-0500 (CentrPMl DPM2014lig3t Ti19e)
Why does the second line not work correctly, even though it appears to be property following the syntax?
Am I to understand that the correct syntax is:
{{ date_expression | date : format}}
You appear to be correct. The documentation is wrong, and the correct syntax is in fact {{date_expression|date:format}
.
Some background on this error: filters in Angular are implemented as basic functions, so you could imagine it as something like function date(date, format)
. However, when used as a filter, the first parameter is actually the value before the pipe. (in this case, data_expression
)
You should submit a pull request to improve this doc. Otherwise I will and I'll take the credit for finding the issue :)