diff --git a/src/index.ts b/src/index.ts index da7efec4..bb3d92aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ import {initAngular1} from './ng1'; -initAngular1(); const DEVICE_READY_TIMEOUT = 2000; @@ -188,6 +187,8 @@ window['IonicNative'] = { WebIntent: WebIntent }; +initAngular1(window['IonicNative']); + // To help developers using cordova, we listen for the device ready event and // log an error if it didn't fire in a reasonable amount of time. Generally, // when this happens, developers should remove and reinstall plugins, since diff --git a/src/ng1.ts b/src/ng1.ts index 1101d257..cd3bc0ce 100644 --- a/src/ng1.ts +++ b/src/ng1.ts @@ -1,24 +1,28 @@ declare var window; /** - * Initialize the ngCordova Angular module if we're running in ng1 + * Initialize the ngCordova Angular module if we're running in ng1. + * This iterates through the list of registered plugins and dynamically + * creates Angular 1 services of the form $cordovaSERVICE, ex: $cordovStatusBar. */ -export function initAngular1() { +export function initAngular1(plugins) { if (window.angular) { - window.angular.module('ngCordova', []); + window.angular.module('ionic.native', []); + + for(var name in plugins) { + let serviceName = '$cordova' + name; + let cls = plugins[name]; + + (function(serviceName, cls, name) { + window.angular.module('ionic.native').service(serviceName, [function() { + let funcs = {}; + for (var k in cls) { + funcs[k] = cls[k]; + } + funcs['name'] = name; + return funcs; + }]) + })(serviceName, cls, name); + } } } - -/** - * Publish a new Angular 1 service for this plugin. - */ -export function publishAngular1Service(config: any, cls: any) { - let serviceName = '$cordova' + cls.name; - console.log('Registering Angular1 service', serviceName); - window.angular.module('ngCordova').service(serviceName, [function() { - let funcs = {}; - for (var k in cls) { - } - return funcs; - }]); -} diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index 500479c8..533217c5 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -1,7 +1,5 @@ import {get} from '../util'; -import {publishAngular1Service} from '../ng1'; - declare var window; declare var Promise; declare var $q;