我的DI错误在哪里?

时间:2017-06-04 19:04:33

标签: angular dependency-injection

我尝试制作一个过滤的指南列表,这些指南作为JSON对象检索。但是在运行代码后我得到DI错误。完全不知道错误可能出现在哪里?有人有想法吗?

错误:

  

error_handler.js:54 EXCEPTION:未捕获(承诺):错误:DI错误   错误:DI错误       at NoProviderError.ZoneAwareError(http://localhost:8080/polyfills.bundle.js:3423:33)       在NoProviderError.BaseError [作为构造函数](http://localhost:8080/vendor.bundle.js:27156:16)       在NoProviderError.AbstractProviderError [作为构造函数](http://localhost:8080/vendor.bundle.js:55463:16)       在新的NoProviderError(http://localhost:8080/vendor.bundle.js:55525:16)       在ReflectiveInjector _。 throwOrNull(http://localhost:8080/vendor.bundle.js:74856:19)       在ReflectiveInjector getByKeyDefault(http://localhost:8080/vendor.bundle.js:74895:25)       在ReflectiveInjector getByKey(http://localhost:8080/vendor.bundle.js:74827:25)       在ReflectiveInjector .get(http://localhost:8080/vendor.bundle.js:74696:21)       在AppModuleInjector.NgModuleInjector.get(http://localhost:8080/vendor.bundle.js:56399:52)       在CompiledTemplate.proxyViewClass.AppView.injectorGet(http://localhost:8080/vendor.bundle.js:75631:45)       在CompiledTemplate.proxyViewClass.DebugAppView.injectorGet(http://localhost:8080/vendor.bundle.js:76059:49)       在ElementInjector.get(http://localhost:8080/vendor.bundle.js:75135:27)       在ReflectiveInjector _。 getByKeyDefault(http://localhost:8080/vendor.bundle.js:74892:24)       在ReflectiveInjector getByKey(http://localhost:8080/vendor.bundle.js:74827:25)       在ReflectiveInjector .get(http://localhost:8080/vendor.bundle.js:74696:21

以下是代码的摘录:

import { Component, OnInit, ElementRef, Injectable } from '@angular/core';
import { FormControl, FormArray, FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Recommendation, Question, PreReq, Topic } from '../recommendations/recommendation';

import { RecommendationService } from '../recommendations/recommendation.service';

@Component({
  selector: 'app-inputform',
  templateUrl: './inputform.component.html',
  styleUrls: ['./inputform.component.css']
})


export class InputformComponent implements OnInit{
  inputform: FormGroup;
  Recommendations: Recommendation[];
  Guidelines: string[] = [];
  Topics: string [] = [];
  Questions: string [] = [];
  public query = '';
  public filteredList = [];
  public elementRef;
  selectedIdx: number;

  constructor(private fb: FormBuilder, myElement: ElementRef, private RecommendationService: RecommendationService) {
    this.elementRef = myElement;
    this.selectedIdx = -1;
    this.createForm();
  }

  ngOnInit() {
  this.RecommendationService
        .getRecommendations()
        .then((Recommendations: Recommendation[]) => {
        this.Recommendations = Recommendations.map((Recommendation) => {
          let checker = 0;
          console.log(Recommendation.guideline);
          for (let i = 0; i < this.Guidelines.length; i++) {
            if (Recommendation.guideline == this.Guidelines[i]) {
               checker = 1;
            }
          }
          if (checker == 0) {
            this.Guidelines.push(Recommendation.guideline);
          }
          checker = 0;

          return Recommendation;
        });
      });

这是模块定义:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { ReactiveFormsModule } from '@angular/forms';  // <-- #1 import module voor reactieve forms
import { AppComponent } from './app.component';
import { RecommendationDetailsComponent } from './recommendations/recommendation-details/recommendation-details.component';
import { RecommendationListComponent } from './recommendations/recommendation-list/recommendation-list.component';
import { RecommendationSearchComponent } from './recommendations/recommendation-details/recommendation-search';
import { AppRoutingModule } from './app-routing.module';
import { GuidelinepageComponent } from './guidelinepage/guidelinepage.component';
import { InputformComponent } from './inputform/inputform.component';

@NgModule({
  declarations: [
    AppComponent,
    RecommendationDetailsComponent,
    RecommendationListComponent,
    RecommendationSearchComponent,
    GuidelinepageComponent,
    InputformComponent,
  ],
  imports: [
    AppRoutingModule,
    BrowserModule,
    FormsModule,
    HttpModule,
    ReactiveFormsModule 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

1 个答案:

答案 0 :(得分:0)

您似乎没有提供RecommendationService

尝试将模块代码中的providers部分修改为:

providers: [ RecommendationService ],