AngularJS Live Example
by bullrout
HTML
<script src="https://code.angularjs.org/1.3.9/angular.min.js"></script>
<!doctype html>
<html ng-app="HelloApp">
<body>
<hello-world value="nyFieldValue"></hello-world>
<div id="test"></div>
<div id="test2"></div>
</body>
</html>
JavaScript
angular.module('components', [])
.directive('helloWorld', function () {
return {
restrict: 'E',
scope: { value: '=value' },
template: '<input type="text" data-ng-model="value" data-ng-change="check()">',
link: function( scope , element , attributes ) {
scope.check = function() {
if (scope.value.indexOf('.') === -1) {
angular.element(document.querySelector('#test')).text(scope.value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
angular.element(document.querySelector('#test2')).text(typeof Number(scope.value));
} else if (scope.value.indexOf('.') !== -1) {
angular.element(document.querySelector('#test')).text(scope.value.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'));
angular.element(document.querySelector('#test2')).text(typeof parseFloat(scope.value));
}
}
}
}
})
angular.module('HelloApp', ['components'])