35

Experiencing the same problem with the date, percent, and currency pipes when using them in a template—

For example, I'm using a simple percent pipe:

{{ .0237| percent:'1.2-2' }}

It works when running on Chrome, but when I deploy to an iOS device, it throws this error:

"EXCEPTION: ReferenceError: Can't find variable: Intl in [{{ {{ .0237| percent:'1.2-2' }} ..."

Has anyone else run into this problem? Any suggestions or help would be greatly appreciated! Thanks!

Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
Hunter
  • 405
  • 2
  • 5
  • 10

4 Answers4

69

That's because it relies on the internalization API, which is not currently available in all browsers (for example not on iOS browser).

See the compatibility table.

This is a known issue (beta.1).

You can try to use a polyfill for Intl.

To do so, you can use the CDN version, and add this to your index.html:

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>

Or if you use Webpack, you can install the Intl module with NPM:

npm install --save intl

And add these imports to your app:

import 'intl';
import 'intl/locale-data/jsonp/en';
cexbrayat
  • 17,772
  • 4
  • 27
  • 22
  • 3
    i recommend install via npm because if you have network problem then can't access polyfill via internet and app show blank screen – vuhung3990 Sep 09 '16 at 09:03
  • 2
    Add the two import statements to your polyfills.ts if you're using the CLI build setup – Per Hornshøj-Schierbeck Sep 28 '16 at 08:20
  • I've been chasing this bug down for days, where ngFor would not refresh correctly on iOS9.3.1.1 because of this JS bug, which I found in Safari. I can confirm what @davidfischer said. – vr_driver May 15 '18 at 14:25
11

There is a quick fix for this. Add

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>

to your index.html file before the <script src="cordova.js"></script> entry.

See this github answer https://github.com/angular/angular/issues/3333#issuecomment-203327903

Stevo
  • 2,601
  • 3
  • 24
  • 32
1

Or another solution would be writing those pipes yourself. For the date, for example, you can use moment.js, or here is an example for the currency pipe:

import { Pipe, PipeTransform } from '@angular/core'

@Pipe({ name: 'currency' })

export class CurrencyPipe implements PipeTransform {
    constructor() {}

    transform(value: string, currencyString: string ) { 
        let stringOnlyDigits  = value.replace(/[^\d.-]/g, '');
        let convertedNumber = Number(stringOnlyDigits).toFixed(2);
        return convertedNumber + " " + currencyString;
    }
} 

This pipe is transforming the currency. The percent pipe would work nearly the same way. The regex is filtering all digits including the point for float numbers.

Yax
  • 70
  • 8
Highriser
  • 212
  • 2
  • 14
0

Here's what I did with RC3. I think it will work with RC4 too.

Create a pipe by typing ionic g pipe pipe-transform

Clean the code to remove @Injectable. Also, use camel-case to name it like below. Implement PipeTransform.

import { Pipe, PipeTransform} from '@angular/core';

/**
 * Takes a number and transforms into percentage upto
 * specified decimal places
 *
 * Usage:
 * value | percent-pipe: decimalPlaces
 *
 * Example:
 * 0.1335 | percent-pipe:2
*/
@Pipe({
  name: 'percentPipe'
})
export class PercentPipe implements PipeTransform {
  /**
   * Takes a number and returns percentage value
   * @param value: number
   * @param decimalPlaces: number
   */
  transform(value: number, decimalPlaces:number) {
    let val = (value * 100).toFixed(decimalPlaces);
    return val + '%';
  }
}

In your app.module add to declarations array.

Then in the html use it like in the example usage above. Here's my example

 <ion-col *ngIf="pd.wtChg < -0.05  || pd.wtChg > 0.05" width-25 highlight>
    {{pd.wtChg | percentPipe: 2}}
  </ion-col>

Notice I'm using an *ngIf and a highlight directive too in case someone needs extra help. Not necessary obviously.

Resulting image

Mukus
  • 4,870
  • 2
  • 43
  • 56