-2

Reading the official docs I'm confused about how to initialize pipes in Angular.

When I read the docs where it says

{{ value_expression | currency [ : currencyCode [ : display [ : digitsInfo [ : locale ] ] ] ] }}

I interpret this as I should be able to get the following to work

// component.ts
import { CurrencyPipe } from '@angular/common'; 
// component.html
<div>{{ myNumber | CurrencyPipe [ : "USD" [ : "symbol" [ : "0.0-0" ] ] ] }}</div>

But obviously this doesn't work.

Two insights would fully answer my question: 1) an explanation for what the doc notation is trying to say, and 2) how I can implement my desired pipe


Here's an minimal reproducible example (mre). I'm working in a larger project so if more information is needed let me know.

// mre.component.ts
import { CurrencyPipe } from '@angular/common'; 
import { Component } from '@angular/core';

@Component({
  selector: 'mre',
  templateUrl: './mre.component.html',
  styleUrls: ['./mre.component.css'] // This is a blank file
});
export class MreComponent {
  constructor() {
      console.log(CurrencyPipe); // This line is only here so typescript won't be angry
  };
  public myNumber: number = 1234.56;
}
// mre.component.html
<div>
     <p>{{ myNumber | currency }}</p>
</div>

If I include BrowserModule in mre.module.ts then I get the following error at runtime. Error: BrowserModule has already been loaded.


I removed Browser Module (to have it how it was) and it works now. Apparently I changed something between those steps but I don't know what it was. Thanks for the help. Sorry for not being descriptive enough with the cases I'd tried beyond the example I initially gave.

financial_physician
  • 1,672
  • 1
  • 14
  • 34
  • Look at the actual examples, e.g. `

    B: {{b | currency:'CAD':'symbol':'4.2-2'}}

    `. The square brackets are to indicate that they're optional, see https://stackoverflow.com/q/10925478/3001761.
    – jonrsharpe Jul 13 '21 at 15:49
  • @jonrsharpe yeah, I read that too but that still doesn't explain how to initialize the variable `currency`. If I replace my`
    ` element for your `

    ` element, it still doesn't run because currency isn't defined. How are you supposed to initialize currency?

    – financial_physician Jul 13 '21 at 15:57
  • What do you mean _"initialize the variable currency"_? That's the name of the pipe. If it's not working with the documented syntax, give a [mre] of that - as long as you're loading the `BrowserModule`, it should be available in the template. – jonrsharpe Jul 13 '21 at 16:10
  • @jonrsharpe BrowserModule is imported and it still doesn't work. I'll try putting together a minimal reproducible example – financial_physician Jul 13 '21 at 16:24
  • Now it works... Hmmm thanks! – financial_physician Jul 13 '21 at 16:39

1 Answers1

1

You don't need to initialize the currency pipe, it is built into Angular. You just need to make sure that your module file is importing CommonModule. In your example your syntax is a little off. To ensure that you have correctly imported the pipe try just this,

<div>{{ myNumber | currency }}</div>

This will use the currency pipe with all of the default formatting. Then once you have that working you can start adding some of the formatting parameters,

<div>{{ myNumber | currency:'USD':'code':'.2-2' }}</div>

The different values you can supply for each of the formatting options can be found here

David Kidwell
  • 600
  • 1
  • 6
  • 15
  • 1
    @dkidell is correct unless you do if you want to use the pipe in the component. In which case you will need inject it into your ctor..and use the .transform method that is exposed. – Darren Street Jul 13 '21 at 16:42
  • Importing `CommonModule` and rebuilding the project fixed it. I don't think I read anything about CommonModule when I reading docs so idk how long it would have taken me to figure this out – financial_physician Jul 13 '21 at 17:20