I'm trying to call a service from a particular class, but for some reason it can't be called, even though I am able to use it in other classes. I'm using @Input. I don't know if this is causing the problem.
Code:
import { Component, Input, OnInit } from '@angular/core';
import { Category } from './../Category';
import { CertService } from './../../shared/Service/cert.service';
@Component({
selector: 'app-cert-item',
templateUrl: './cert-item.component.html'
})
export class CertItemComponent implements OnInit {
@Input() category: Category;
@Input() categoryId: number;
constructor(
private certService: CertService //Console error "No provider for CertService!"
) { }
ngOnInit() {
console.log(this.certService.getCategories());
}
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { routing } from './app.routing';
import { ReactiveFormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AppComponent } from './app.component';
import { HeaderComponent } from './shared/header.component';
import { DropdownDirective } from './dropdown.directive';
import { CertsComponent } from './certs/certs.component';
import { CertItemComponent } from './certs/cert-category/cert-item.component';
import { CertService } from './shared/service/cert.service';
import { HttpService } from './shared/Service/http.service';
import { LoginComponent } from './login/login.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
DropdownDirective,
CertsComponent,
CertItemComponent,
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing,
ReactiveFormsModule,
NgbModule.forRoot()
],
providers: [
CertService,
HttpService
],
bootstrap: [AppComponent]
})
export class AppModule { }
Service class I'm trying to call (sample):
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from "@angular/http";
import { Observable } from "rxjs/Rx";
import 'rxjs/Rx';
@Injectable()
export class CertService {
constructor(private http: Http) { }
getCategories() {
return this.http.get('api/categories')
.map((response: Response) => response.json());
}
}
I also can't get the current route, using:
this.subscription = this.route.params.subscribe(
(params) => {...
...});
Again, the code above works fine in other classes but not in this class.
This will happen if something wrong was provided as a provider. And it is unlikely that there may be another reason for this.
In one place it is ...shared/service/cert.service, and in another it is ...shared/Service/cert.service. Case matters. It depends on the environment what happens with these modules during build process, but this can cause errors, e.g. they may become duplicated and refer to different objects.
To quickly debug this, CertService can be exposed from both files as window.CertService1 and window.CertService2 and compared in console if both are defined and equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With