I am trying to create a custom pipe to access the data from JSON file and use the data in ts file. I have a issue on how to access the data outside a function.
@Pipe({ name: 'currency' })
export class CurrencyPipe implements PipeTransform {
constructor(private httpService: HttpClient) { }
arrCurrencies: any;
transform(value, country): string {
if (!value) {
return ''
} else {
if (!country) {
country = 'NZD'
}
const symbol = symbols[country] //value of symbol is $
if (symbol) {
this.httpService.get('./assets/locales/en_AU.json').subscribe(
data => {
this.arrCurrencies = data;
});
console.log('this.arrCurrencies', this.arrCurrencies); // not able to access arrCurrencies outside the func.
return accounting.formatMoney(value, { symbol, format: '%s %v',precision : this.arrCurrencies.CURRENCYFORMATS.AUD.CURRENCYDECIMALS , thousand: ",",
decimal : "." })
}
return accounting.formatMoney(value, { symbol: country, format: '%v %s' })
}
}
}
JSON:
"CURRENCYFORMATS": {
"DEFAULT": {
"DECIMALSEPARATOR": ".",
"THOUSANDSEPARATOR": ",",
"CURRENCYDECIMALS": "2"
},
"AUD": {
"DECIMALSEPARATOR": ".",
"THOUSANDSEPARATOR": ",",
"CURRENCYDECIMALS": "4"
},
}