RAHUL DEEP
RAHUL DEEP

Reputation: 655

Provider inside provider in Ionic2/ Angular2 Application

I am having two provider.

  1. AppStorage which is responsible for setting and getting values from storage.

    import { Injectable } from '@angular/core';
    import {Http, Headers, RequestOptions} from '@angular/http';
    import {Observable} from 'rxjs/Rx';
    import {Storage} from '@ionic/storage';
    
    @Injectable()
    export class AppStorage {
    
      constructor(public http: Http, private storage:Storage) {
        console.log('Hello Appstorage Provider');
      }
      setValue(key,value){
        var setPromise = new Promise((resolve,reject)=>{this.storage.set(key,value).then((res)=>{
          return resolve(res);
        })
      });
        return setPromise;
      };
      getValue(key){
        var getPromise = new Promise((resolve,reject)=>{this.storage.get(key).then((val)=>{
          return resolve(val);
          });
        });
        return getPromise;
    
      } 
    }
    
  2. OauthService which is my api service.

    import {Http, Headers, RequestOptions} from '@angular/http';
    import { Injectable, Inject} from '@angular/core';
    import 'rxjs/add/operator/map';
    import { AppStorage } from '../providers/appstorage';
    
    @Injectable()
        export class OauthService{
        http: Http;
        response :any;
        appStorage: AppStorage;
        static get parameters() {
            return [[Http]];
        }
        constructor(http: Http,@Inject(AppStorage) appStorage: AppStorage){
        this.http = http;
            this.appStorage = appStorage;
            setTimeout(()=>{
                console.log('Hello OauthService Provider'+this.appStorage+"===="+this.http);
            },3000)
         //output - >Hello OauthService Providerundefined====[object Object] 
        }
      }
    

Now i am adding both the injectable to my app.module

import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { LoginPage } from '../pages/login/login';
import { AppStorage } from '../providers/appstorage';
import { Storage } from '@ionic/storage';
import { OauthService } from '../providers/oauthservice';

@NgModule({
  declarations: [
    MyApp,
    LoginPage
  ],
  imports: [
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    LoginPage
  ],
  providers: [{provide: ErrorHandler, useClass: IonicErrorHandler},OauthService, Storage, AppStorage]
})
export class AppModule {}

But when i am running the app, the appStorage object in constructor of oauthService is coming as undefined. can you please tell me what mistake i am doing, i am just injecting service inside another service.

Upvotes: 0

Views: 2003

Answers (1)

Mete Cantimur
Mete Cantimur

Reputation: 1401

Your static parameter getter (parameters) in OauthService provider should include all the providers you inject into the constructor. However, you have injected just the Http provider but not AppStorage provider. I have modified the code accordingly below:

import { Http } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { AppStorage } from './appstorage';

@Injectable()
export class OauthService {

    response :any;

    static get parameters() {
        return [[Http], [AppStorage]];
    }

    constructor(private http: Http, private appStorage: AppStorage) {

        setTimeout(() => {
            console.log('Hello OauthService: appStorage=' + this.appStorage + ", http=" + this.http);
        }, 3000)
        //output - >Hello OauthService: appStorage=[object Object], http=[object Object]
    }
}

Upvotes: 4

Related Questions