0

I created a Directive in Angular7 but when i need to use that it show me this error :

Error: StaticInjectorError(AppModule)[NgForOf -> TemplateRef]: StaticInjectorError(Platform: core)[NgForOf -> TemplateRef]: NullInjectorError: No provider for TemplateRef! Error: StaticInjectorError(AppModule)[NgForOf -> TemplateRef]: StaticInjectorError(Platform: core)[NgForOf -> TemplateRef]:

I create this directive in SharedModule :

@NgModule({
  declarations: [FilderrorComponent, UploadfileComponent, ImageComponent, ValidatePermissionDirective],
  imports: [
    CommonModule
  ],
  exports: [ FilderrorComponent, UploadfileComponent, ImageComponent, ValidatePermissionDirective]
})
export class SharedModule {

    static forRoot(): ModuleWithProviders {
      return {
        ngModule: SharedModule,
        providers: [ FilderrorComponent, UploadfileComponent, ImageComponent , ValidatePermissionDirective]
      };
  }
}

and this is my Directive:

@Directive({
  selector: '[appValidatePermission]'
})
export class ValidatePermissionDirective implements OnInit {

  show: boolean;
  constructor(private templateRef: TemplateRef<any>,
              private viewContainerRef: ViewContainerRef
    ,         private dynamic: DynamicPermissionService) { }

  // tslint:disable-next-line:no-input-rename
  @Input() AccessName: string;

  ngOnInit() {
    this.ValidatePemission();
    if (this.show) {
      this.viewContainerRef.createEmbeddedView(this.templateRef);
    } else {
      this.viewContainerRef.clear();
    }
  }
  ValidatePemission() {
    console.log('AccessName : ', this.AccessName);
    const find = this.dynamic.dynamicModel.find(x =>
      !! x.actionsVM.find(z => z.actionEnglishName === this.AccessName));
    console.log(find);
    if (find) {
        console.log(true);
        this.show = true;
      } else {
        console.log(false);
        this.show = false;
      }
  }
}

and i define the shareModule in AdminModule :

@NgModule({
  declarations: [],
  imports: [
    SharedModule,
    AdminpanelRoutingModule,

  ],
  providers: [Toolkit, { provide: HTTP_INTERCEPTORS, useClass: RequestInterceptor, multi: true }]
})
export class AdminpanelModule { }

and I use Directive in HTML :

<span [appValidatePermission]="CreateRole">
  <router-outlet></router-outlet>
</span>

Whats The Problem??? How Can I Solve That?

Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59
mr-dortaj
  • 782
  • 4
  • 11
  • 28

1 Answers1

0

I think the problem comes from the fact that templateRef is null. Try intializing it before using it maybe