Tutorial Create Angular 4 Currency Format Directive For Textbox

Angular 4 tutorial how to create currency directive. Lately, I am studying about angular 4 because there is an ERP project in the company where I work. Creating a currency formatter on the angular is different from the JQuery I’ve discussed in the How to Create Jquery Plugin for Input Currency article. But if you’re used to Angular 4, making currency directive is pretty easy, and the use is more efficient than jquery. Consider the following tutorial.

This simple directive is used to identify input field with the custom currency format. To create a currency format in the input field requires directive and pipe on angular 4. This article will create a custom currency like the following gif image

Angular 4 Currency Format Directive

Before starting the main topic, I will discuss a bit about what is directive and why we need a pipe?

# Directive

An angular directive is a function executed by Angular when Angular finds Directive in DOM (Document Object Model), the function can almost do everything like hiding element before the page loaded, repeat, execute an expression, etc.

There are two types of directive: built-in directive and custom directive. To create this angular directive in the text field, we need a custom directive.

# Pipe

The pipes are similar to Filters in AngularJs (Angular 1). The pipe is used to format output on HTML templates. Angular 4 also provides built-in pipe such as uppercase, lowercase, currency, Datepipe. In this tutorial, we will create a custom pipe so that the currency format can run well.

Features angular currency input that we will build

  • Only number in input typetext
  • When element does not get focus/blur, display value in input box will become format currency
  • When the element gets the focus, the formatted currency in the text field will disappear
  • When the element gets the focus, all the values in the text box will be selected

Angular 4 Currency Format Directive Min

Tutorial Angular 4 Format Currency In Inputs Value In Angular 4

Note: In the following example I will create a simple directive for Indonesian format currency (Rupiah)

Make a custom directive and a custom pipe

# Custom Pipe

Create a new pipe using an angular-cli with the following command

Edit mycurrency.pipe.ts and copy the following code

# Custom Directive

Create a new directive using an angular cli with the following command

Edit mycurrency.directive.ts and copy the following code

To use it on the component, please import the pipe first on your component.ts file as shown below

Then open component.html, add appmycurrency as an attribute on the textbox element you want to provide the currency filter

For live demo app

To change the Indonesian format to $ or your country currency change from the mycurrency.pipe.ts in the constructor class

Read another article: How to Create Autofocus Directive Certain Elements Angular 4

Thus my article about create angular directive to format currency, hope useful.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.