Angular 自定义角度指令:NullInjectorError:StaticInjectorError(AppModule)[MatInput->;ElementRef]

Angular 自定义角度指令:NullInjectorError:StaticInjectorError(AppModule)[MatInput->;ElementRef],angular,angular-material,Angular,Angular Material,由于以下错误,我的Angular应用程序无法编译。我不知道为什么。我刚升级到Angular 8,但我已经更新并重新安装了材质依赖项 NullInjectorError:StaticInjectorError(AppModule)[MatInput -> ElementRef]: StaticInjectorError(Platform: core) [MatInput -> ElementRef]: NullInjectorError: No provider for E

由于以下错误,我的Angular应用程序无法编译。我不知道为什么。我刚升级到Angular 8,但我已经更新并重新安装了材质依赖项

NullInjectorError:StaticInjectorError(AppModule)[MatInput -> ElementRef]: 
 StaticInjectorError(Platform: core) [MatInput -> ElementRef]: 
   NullInjectorError: No provider for ElementRef!
更新

好的,这显然是因为我创建了一个指令。如果我删除了对指令的引用,那么错误就被解决了。我的指令中一定有注射问题

文件位置是否会弄乱初始配置?例如,命令行在应用程序根目录中生成一个服务。我已将我的服务移动到“服务”文件夹。这会是一个问题吗

这是我用来构建这个指令的教程

/app.component.html-这是我调用该指令的原因

<div>
  <app-compliance-review [myRemoveIfFeatureOff]="'compliancerev'" compliancerev="true"></app-compliance-review>
</div>
服务/功能标志.service.ts

import { Directive, ElementRef, Input, OnInit } from '@angular/core';
import { FeatureFlagService } from '../services/feature-flag.service';

@Directive({
  selector: '[myRemoveIfFeatureOff]'
})
export class MyRemoveIfFeatureOffDirective implements OnInit {
  @Input('myRemoveIfFeatureOff') featureName: string;

  constructor(private el: ElementRef,
              private featureFlagService: FeatureFlagService) {
  }

  ngOnInit() {
    if (this.featureFlagService.featureOff(this.featureName)) {
      this.el.nativeElement.parentNode.removeChild(this.el.nativeElement);
    }
  }
}
import { Injectable, ElementRef } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class FeatureFlagService {

  constructor(private elementRef:ElementRef){}

  featureOff(featureName: string) {
    // Read the value from the root element
    if (this.elementRef.nativeElement.hasOwnProperty(featureName)) {
      let sFlag: string = this.elementRef.nativeElement.getAttribute(featureName);
      let bFlag: boolean = (sFlag =="true");
      return !bFlag;
    }
    return true; // if feature not found, default to turned off
  }

  featureOn(featureName: string) {
    return !this.featureOff(featureName);
  }
}
StackTrace:

这可能是因为您没有将
MatInputModule
导入模块:

@NgModule({
  imports: [MatInputModule]
})
export class AppModule {}

不能将
ElementRef
注入角度服务

ElementRef
可以注入指令和组件中,因为它们与DOM中的单个元素相关联。服务不与任何给定元素关联,因此注入
ElementRef
将失败


如果需要从服务函数访问
ElementRef
实例,则需要将引用作为函数参数传递。

我在使用
npm link
时遇到平台核心和提供程序问题。你的节点模块有链接吗?@Reactgular,我自己没有链接任何模块。我怎么能再检查一遍呢?你会记得的,但这里有帮助。尝试更改
private el:ElementRef
private el:ElementRef
。不确定这是否有帮助。在节点\模块上没有链接。我正在根模块中导入此模块。我已经提供了关于我的问题的最新信息。