我应该在哪个模块中声明Angular Directive?

时间:2018-08-25 07:12:59

标签: angular typescript angular-routing angular-module

我有一个指令:

@Directive({
    selector: '[ICheck]',
})
export class ICheckDirective {

    @Input()
    ICheck: string;

    constructor(element: ElementRef) {
        var $element: any = $(element.nativeElement);

        $element.iCheck({
            checkboxClass: 'icheckbox_md',
            radioClass: 'iradio_md',
            increaseArea: '20%'
        })
    }

}

我在app.module中声明的位置

@NgModule({
    declarations: [
        ...
        ICheckDirective,
    ],
    imports: [
        ...
    ],
    providers: [
        ...
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

RoleListComponent的组件roles.module中出现错误

@NgModule({
    imports: [
        RoleListComponent,
        ...
    ],
    declarations: [
        ...
    ]
})
export class RolesModule { }

我遇到错误

  

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

我的路线:

const routes: Routes = [
    {
        path: '',
        children: [
            {
                path: '',
                component: DashboardComponent
            },
            {
                path: 'users',
                loadChildren: './user/user.module#UserModule'
            },
            {
                path: 'roles',
                loadChildren: './roles/roles.module#RolesModule'
            },
            {
                path: 'portalpageowners',
                loadChildren: './portalpageowner/portalpageowner.module#PortalPageOwnerModule'
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class AdminRoutingModule { }

1 个答案:

答案 0 :(得分:4)

您需要添加到要在其中使用该模块的声明中。根据您的情况,进入DELIMITER $$ CREATE PROCEDURE function1(query1, query2, query3) BEGIN DECLARE EXIT HANDLER FOR SQLEXCEPTION,SQLWARNING rollback; start transaction; // I want to execute query 1 here // I want to execute query 2 here // I want to execute query 3 here commit; END 。但是,如果您需要将其添加到更多模块中,则需要创建一个RoleModule,并从中导出该指令

SharedModule

并将此模块导入其他模块。

@NgModule({
   declarations: [ ..., ICheckDirective, ... ],
   exports: [ ..., ICheckDirective, ... ]
})
export class SharedModule { }

@NgModule({ imports: [ ..., SharedModule, ... ] }) export class RoleModule { } ... @NgModule({ imports: [ ..., SharedModule, ... ] }) export class AnotherModule { } 将使该指令对那些导入export的模块的声明可见。