Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Promise rejection: No provider for HTTPService

I am migrating my app to the latest RC and am getting some errors that I cannot fix. Before deciding to ask here I made a thorough search but without any luck.

So, when pressing a button the following should load:

Here is the component:

import { Component,OnInit } from '@angular/core';

import { HTTPService } from '../shared/api.service';
import { Server } from './server.model';
import { SharedService } from '../shared/moveData.service';


@Component({
    templateUrl: './mainserverpage.template.html',
})
export class MainServerPage implements OnInit {

    constructor(private _httpService: HTTPService, 
                private _moveData: SharedService) { }

    errorMessage: string;
    public servers: Server[];
    isLoading = true;
    tillLoading;
    selectedServer: Server;
    currentServer;
    isServerOnline=false;

    ngOnInit() {
          this.getServers('qa');
    }

    reloadServers(env) {
        this.servers = null;

        this.getServers(env);
    }

    getServers(env?) {
        this._httpService.getServers(env)
            .subscribe(
            value => {
                this.servers = value;
                this.isLoading = false;
            },
            error => this.errorMessage = <any>error);
    }
}

The module is pretty basic:

import { NgModule }            from '@angular/core';
import { CommonModule }        from '@angular/common';
import { HttpModule }          from '@angular/http';

import { SharedModule }        from '../shared/shared.module';

import { MainServerPage }      from './mainserverpage.component';
import { Server }         from './server.model';
import {HTTPService} from '../shared/api.service';

@NgModule({
    imports: [
        CommonModule,
        SharedModule,
        HttpModule,
    ],
    declarations: [
        MainServerPage
    ],
    exports: [
        MainServerPage
    ],
    providers: [
        HTTPService
    ]
})
export class MainserverpageModule {
}

The routing:

import { RouterModule  }     from '@angular/router';

import { MainServerPage }    from './mainserverpage.component';


export const mainRouting = RouterModule.forChild([
    { path: 'mainserverpage', component: MainServerPage }
]);

And finally the app.module that I am using. I see no sense in adding the template.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { SharedModule } from '../app/shared/shared.module';
import { MainserverpageModule } from '../app/servers/mainserverpage.module';

import { HomeComponent }     from './home.component';
import { NavBarComponent }   from './navbar.component';
import { NotFoundComponent } from '../app/shared/notfound.component';
import { MainServerPage } from '../app/servers/mainserverpage.component';

import { routing }           from './app.routing';
import { mainRouting } from '../app/servers/mainserverpage.routing';

@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    HomeComponent,
    NotFoundComponent,
    MainServerPage
  ],
  imports: [
    BrowserModule,
    FormsModule,
      HttpModule,
    SharedModule,
    routing,
    mainRouting
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Here is the error that I am receiving when I am trying to load this:

zone.js:420 Unhandled Promise rejection: No provider for HTTPService! ; Zone: angular ; Task: Promise.then ; Value: NoProviderError {__zone_symbol__error: Error: DI Error at NoProviderError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:87143:…, _nativeError: ZoneAwareError, keys: Array[1], injectors: Array[1], __zone_symbol__message: "No provider for HTTPService!"…}__zone_symbol__error: Error: DI Error at NoProviderError.ZoneAwareError

Could be something simple for you but from here I can't see the problem. Thank you.

like image 487
raz dame Avatar asked Jan 26 '17 13:01

raz dame


2 Answers

You need to add your MainserverpageModule to your app.module imports. Currently you've only included the MainServerPagecomponent and not your service.

@NgModule({
  declarations: [
    AppComponent,
    NavBarComponent,
    HomeComponent,
    NotFoundComponent
    //Remove the component
  ],
  imports: [
    BrowserModule,
    MainserverpageModule <-- Add the module
    FormsModule,
    HttpModule,
    SharedModule,
    routing,
    mainRouting
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
like image 158
DGarvanski Avatar answered Sep 29 '22 08:09

DGarvanski


You need to tell your component about the providers using

import { SharedService } from '../shared/moveData.service';

@Component({
    templateUrl: './mainserverpage.template.html',
    providers: [SharedService]   <= inject your services here
})
export class MainServerPage implements OnInit {

    constructor(private _httpService: HTTPService, 
                private _moveData: SharedService) { }

}
like image 33
Ali Baig Avatar answered Sep 29 '22 07:09

Ali Baig