4

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

错误错误:未捕获(承诺):错误:模板解析错误:无法绑定到“appHasAccess”,因为它不是“输入”的已知属性。

我使用的代码如下:

我已经做了所有可能的尝试。任何人都知道我如何解决这个问题。

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]
        };
    }
}
4

2 回答 2

3

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

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

    import { NgModule }      from '@angular/core';       
    
    @NgModule({
       declarations: [],
       imports: [
           DigiUserRightsModule.forRoot()
       ],
       providers: []
    })
    
    export class UserModule {}
    
于 2018-04-21T06:44:46.397 回答
1
  1. I think you have something wrong with your @Input. The input should look like this:

@Input('appHasAccess'): any

  1. You don't declare the accessDetail = { 'a': 1 } in the directive. This should be your input from the html like this:

    // In HTML
    <div id="myElement" appHasAccess={{accessDetail}}
    
    // In TypeScript
    accessDetail = { 'a': 1 }
    
于 2018-04-20T13:15:09.397 回答