Skip to content

Instantly share code, notes, and snippets.

@cpoDesign
Created May 20, 2016 09:25
Show Gist options
  • Save cpoDesign/d1ccb5320b2229b41181a55818d475db to your computer and use it in GitHub Desktop.
Save cpoDesign/d1ccb5320b2229b41181a55818d475db to your computer and use it in GitHub Desktop.
Angular JS - custom date format
var app = angular.module('angularjs-starter', []);
app.filter('customCurrency', ["$filter", function ($filter) {
return function(amount, currencySymbol){
var currency = $filter('currency');
if(amount<0){
return '-' + currency(amount, currencySymbol).replace('(','').replace(')','');
}
return currency(amount, currencySymbol);
};
}]);
app.controller('MainCtrl', function($scope) {
$scope.amount = -250.75;
});
<!DOCTYPE html>
<html ng-app="angularjs-starter">
<head lang="en">
<meta charset="utf-8">
<title>Custom Plunker</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<link rel="stylesheet" href="style.css">
<script>
document.write('<base href="' + document.location + '" />');
</script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<h1>{{10 | currency}}</h1>
<h1>{{-10 | currency}}</h1>
<h1>{{-10 | currency:"£"}}</h1>
<p>{{amount | currency:"£"}}</p>
<p>{{amount | customCurrency:"£"}}</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment