Skip to content

Commit

Permalink
update code to make it compatible with intl-tel-input version 16.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravsoni119 committed Jul 6, 2019
1 parent 630c50b commit 85c0344
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ npm install ng2-tel-input intl-tel-input --save

After install, you need to add **intlTelInput.css**, **intlTelInput.min.js**, **utils.js**.

In case of @angular/cli, add 3 files in your `angular.json`.
In case of @angular/cli, add 2 files in your `angular.json`.

For example,

Expand All @@ -28,8 +28,7 @@ For example,
```
"scripts": [
...
"node_modules/intl-tel-input/build/js/intlTelInput.min.js",
"node_modules/intl-tel-input/build/js/utils.js",
"node_modules/intl-tel-input/build/js/intlTelInput.min.js"
...
]
```
Expand Down Expand Up @@ -59,6 +58,11 @@ In order to use this directive, you need to add "ng2TelInput" directive with "[n

# Note
**(intlTelInputObject)** returns **intl-tel-input** instance.
By default this package get **utils.js** from below link:-
https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/16.0.1/js/utils.js
But you can also provide your utilsScript file by using below options:-

[ng2TelInputOptions]="{initialCountry: 'in', utilsScript: 'node_modules/intl-tel-input/build/js/utils.js'}"

# How to use this instance?
You can use it perform any functionality that is available on intl-tel-input plugin. **For example**, in your component,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng2-tel-input",
"version": "2.0.2",
"version": "2.0.3",
"description": "An Angular 2 wrapper for intl-tel-input library",
"main": "ng2-tel-input.js",
"scripts": {
Expand Down
17 changes: 14 additions & 3 deletions src/ng2-tel-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Directive, ElementRef, EventEmitter, HostListener, Inject, Input, OnIni
import { isPlatformBrowser } from '@angular/common';

declare const window: any;
const defaultUtilScript = 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/16.0.1/js/utils.js';

@Directive({
selector: '[ng2TelInput]',
})
export class Ng2TelInput implements OnInit {
@Input('ng2TelInputOptions') ng2TelInputOptions: any;
@Input('ng2TelInputOptions') ng2TelInputOptions: any = {};
@Output('hasError') hasError: EventEmitter<boolean> = new EventEmitter();
@Output('ng2TelOutput') ng2TelOutput: EventEmitter<any> = new EventEmitter();
@Output('countryChange') countryChange: EventEmitter<any> = new EventEmitter();
Expand All @@ -16,12 +17,18 @@ export class Ng2TelInput implements OnInit {
ngTelInput: any;

constructor(private el: ElementRef,
@Inject(PLATFORM_ID) private platformId: string) {
@Inject(PLATFORM_ID) private platformId: string) {
}

ngOnInit() {
if (isPlatformBrowser(this.platformId)) {
this.ngTelInput = window.intlTelInput(this.el.nativeElement, this.ng2TelInputOptions);
this.ng2TelInputOptions = {
...this.ng2TelInputOptions,
utilsScript: this.getUtilsScript(this.ng2TelInputOptions)
};
this.ngTelInput = window.intlTelInput(this.el.nativeElement, {
...this.ng2TelInputOptions
});

this.el.nativeElement.addEventListener("countrychange", () => {
this.countryChange.emit(this.ngTelInput.getSelectedCountryData());
Expand Down Expand Up @@ -49,4 +56,8 @@ export class Ng2TelInput implements OnInit {
setCountry(country: any) {
this.ngTelInput.setCountry(country);
}

getUtilsScript(options: any) {
return options.utilsScript || defaultUtilScript;
}
}

0 comments on commit 85c0344

Please sign in to comment.