<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-filter-reverse-production</title>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="fooApp">
<div ng-controller="MyController">
<input as-currency ng-model="amount" /> <br>
Actual Model Value: {{amount}} <br>
</div>
</body>
</html>
<!--
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
(function(angular) {
'use strict';
angular.module('fooApp', [])
.directive('asCurrency', function (currencyFilter) {
return {
restrict: 'A',
require: '?ngModel',
link: function (scope, elem, attrs, ctrl) {
if (!ctrl) return;
var sanitize = function(s) {
return s.replace(/[^\d|\-+|\.+]/g, '');
}
var convert = function() {
var plain = sanitize(ctrl.$viewValue);
ctrl.$setViewValue(currencyFilter(plain));
ctrl.$render();
};
elem.on('blur', convert);
ctrl.$formatters.push(function (a) {
return currencyFilter(a);
});
ctrl.$parsers.push(function(a) {
return sanitize(a);
});
}
};
})
.controller('MyController', function($scope) {
$scope.amount = '25.00';
});
})(window.angular);