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.
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` element, it still doesn't run because currency isn't defined. How are you supposed to initialize currency?