question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CustomLoader: Dependency Injection relying on Ionic (Platform)

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior I am sure I am missing something obvious. I am attempting to set up my own CustomLoader in use in an Ionic 2 app.

The CustomLoader is a little complex in that the loader relies on Cordova/Ionic for the File plugin in order to load a downloaded file we write to.

Essentially the app downloads a language pack which we can use to update translations - but I am writing the loader so it detects if there is a translation present, and to use that.

I have provided the dependency via the deps array:

imports: [
    IonicModule.forRoot(MyApp),
    TranslateModule.forRoot({
      provide: TranslateLoader,
      useFactory: createMyTranslateLoader,
      deps: [MyStorageProvider]
    }),
  ]

MyLoader has a dependency of MyStorageProvider:

class MyLoader implements TranslateLoader {
  constructor(private storage: MyStorageProvider) {
  }

  getTranslation(lang: string): Observable<any> {
    return this.storage.getTranslations();
  }
}

And MyStorageProvider has some dependencies as follows:

import { Platform } from 'ionic-angular';
import { File } from 'ionic-native';

export class MyStorageProvider {

  constructor(private platform: Platform) {
  }

  getTranslations(): Observable<any> {
    ...
  }

}

I am getting the standard Angular 2 error: No provider for MyStorageProvider!.

Normally this is fine, but the fact it’s a root module relying on another root module, makes me think it cannot be done, but my limited knowledge of Angular 2’s inner depths, I’m not sure.

Expected/desired behavior To be able to Inject the Ionic library into a Custom Loader.

Please tell us about your environment:

  • ng2-translate version: 4.2.0

  • Angular version: 2.2.1

  • Browser: [all]

  • Language: [TypeScript 2.0.10]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
kelvindartcommented, Jan 12, 2017

Ace - thanks @SamVerschueren!

0reactions
SamVerschuerencommented, Jan 10, 2017

In order to make sure that the plugins are loaded in my Cordova application, I use this self-written service for that.

import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Observer } from 'rxjs/Observer';

@Injectable()
export class CordovaService {

	private ready$: Observable<boolean>;

	constructor() {
		this.ready$ = new Observable((observer: Observer<boolean>) => {
			document.addEventListener('deviceready', () => {
				observer.next(true);
				observer.complete();
			}, false);
		}).share();
	}

	ready() {
		return this.ready$;
	}
}

Works fine for me.

@Injectable()
export class MyService {

	constructor(
		private cordova: CordovaService
	) { }

	foo() {
		return this.cordova.ready().map(() => ...);
	}
}

Not sure if it could help you, but just wanted to share.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dependency injection in tabs - Ionic Angular
IMHO this is a reasonable strategy, and it works perfectly well with Ionic apps. You're not required to split everything into lazily-loaded ...
Read more >
Dependency Injection in Ionic 2 Application - YouTube
Your browser can't play this video. Learn more. Switch camera.
Read more >
Ionic Navigation and Angular Routing: The Ultimate Guide
This post is part of the "Mastering Ionic Framework" series which deep ... Just import and inject a reference to our custom loader...
Read more >
inject dependency or declare dependency Tags : 1) ionic
An ElementRef is backed by a render-specific element. In the browser, this is usually a DOM element. Because ElementRef is a class you...
Read more >
Open Source Used In slido-test 3.0 - Cisco
1.28 angular-platform-browser-dynamic 13.2.2 ... 1.87 cordova-plugin-ionic-webview 5.0.0 ... 1.412 microsoft-extensions-dependencyinjection 5.0.2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found