Hussein Salman
Hussein Salman

Reputation: 8256

usage of require within angularjs: any alternative

I am tryinng to use google-libphonenumber which is at the following link in my angularjs 1.5 application.

The usage of this library as mentioned in the link:

// Require `PhoneNumberFormat`.
var PNF = require('google-libphonenumber').PhoneNumberFormat;
 
// Get an instance of `PhoneNumberUtil`.
var phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
 
// Parse number with country code.
var phoneNumber = phoneUtil.parse('202-456-1414', 'US');
 
// Print number in the international format.
console.log(phoneUtil.format(phoneNumber, PNF.INTERNATIONAL));

since require is a server side function that is expected to be used with nodejs, its not working with angularjs.

How can i use this in angularjs controller in order to utilize the functions it provide?

Upvotes: 1

Views: 764

Answers (1)

Francois
Francois

Reputation: 3090

You can actually use require in the browser, you would need to use browserify for example. Another way is to include the file dist/libphonenumber.js with a script tag in your page ans use it this way :

var PNF = libphonenumber.PhoneNumberFormat;
...

No magic, I read the source code and if it detects it is in a browser context, it binds libphonenumber to window.

Upvotes: 1

Related Questions