无法绑定到指令 ('appHasAccess'),因为它不是 'input' 的已知 属性

Can't bind to Directive('appHasAccess') since it isn't a known property of 'input'

我创建了一个自定义属性指令库包并安装到 myProject 中,当我尝试使用此自定义指令时出现错误。

ERROR Error: Uncaught (in promise): Error: Template parses errors: Can't bind to 'appHasAccess' since it isn't a known property of 'input'.

我使用的代码如下:

all possible try I have done. any have an idea how I resolve this.

1.指令:HasAccessDirective.ts

@Directive({
  selector: '[appHasAccess]',
})

export class HasAccessDirective {

accessDetail = { 'a': 1 }
 @Input('appHasAccess') set appHasAccess(accessDetail: any) {
    // Based on right control enable/disable
    this.eleRef.nativeElement.disabled = this.appRights.hasRights(accessDetail);
}
constructor(private eleRef: ElementRef,
    private appRights: MyService) { }
}

2。模块:DigiUserRightsModule.ts

   @NgModule({
        declarations: [
            HasAccessDirective
        ],
        imports: [
            CommonModule,
            HttpClientModule,
        ],
        exports: [
            HasAccessDirective
        ],
        providers: [UserRightsService]
    })

export class DigiUserRightsModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: DigiUserRightsModule,
            providers: [UserRightsService]
        };
    }
}
  1. 我认为你的 @Input 有问题。输入应如下所示:

@Input('appHasAccess'): any

  1. 您没有在指令中声明 accessDetail = { 'a': 1 }。这应该是您来自 html 的输入,如下所示:

    // In HTML
    <div id="myElement" appHasAccess={{accessDetail}}
    
    // In TypeScript
    accessDetail = { 'a': 1 }
    

我做了以下更改以使其正常工作。我将我的指令模块注入到我的用户模块而不是应用程序模块。用户模块在它加载的路径上延迟加载并且工作正常。

  • 在用户模块设置我的指令包模块:

    import { NgModule }      from '@angular/core';       
    
    @NgModule({
       declarations: [],
       imports: [
           DigiUserRightsModule.forRoot()
       ],
       providers: []
    })
    
    export class UserModule {}