Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://country.io/phone.json // e.g. DE: “Germany”
- http://country.io/names.json // e.g. DE: “49”
- import { Injectable } from '@angular/core';
- import { Http } from '@angular/http';
- import 'rxjs/Rx';
- import { Observable } from 'rxjs/Observable';
- @Injectable()
- export class CountryService {
- constructor( private _http:Http) { }
- getCountryCode(): Observable<any> {
- return this._http.get('http://crossorigin.me/http://country.io/phone.json')
- .map(countryCodes => countryCodes.json());
- }
- getPhonePrefix(): Observable<any> {
- return this._http.get('http://crossorigin.me/http://country.io/names.json')
- .map(phonePrefix => phonePrefix.json());
- }
- }
- myPhonePrefixObject;
- this.countryPhonePrefix()
- .then((pp) => {
- myPhonePrefixObject = pp;
- })
- .catch((err) => {
- console.log(err);
- });
- private getCountryCode() {
- return new Promise((resolve) => {
- this._countryService.getCountryCode()
- .subscribe(
- res => resolve(res)
- );
- });
- }
- private getPhonePrefix() {
- return new Promise((resolve, reject) => {
- return this._countryService.getPhonePrefix()
- .subscribe(
- res => resolve(res),
- error => reject(error)
- );
- });
- }
- private countryPhonePrefix() {
- return new Promise((resolve, reject) => {
- let cc: Object;
- this.getCountryCode()
- .then((cCode) => {
- cc = cCode;
- return this.getPhonePrefix()
- })
- .then((pPrefix) => {
- let pp: Object = {};
- Object.keys(cc).forEach((key, index) => {
- pp[cc[key]] = pPrefix[key];
- });
- resolve(pp);
- })
- .catch((err) => {
- reject(err);
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement