How to declare a directive globally for all modules?

前端 未结 2 504
温柔的废话
温柔的废话 2020-12-13 13:34

I\'m developing a Github repo which follows the offical tutorial of Angular (Tour of Heroes). You can see all the code here.

My problem, is that I have a directive d

2条回答
  •  再見小時候
    2020-12-13 13:58

    If you need to use the Directive

    @Directive({
      selector: '[appMyCommon]'
    })
    export class MyCommonDirective{}
    

    everywhere you should create a new Module.

    If you use the Angular CLI you can generate:

    ng g module my-common
    

    The Module:

    @NgModule({
     declarations: [MyCommonDirective],
     exports:[MyCommonDirective]
    })
    export class MyCommonModule{}
    

    Important! the exports allow you to use the Directives outside the Module.

    Finally, import that Module in every Module where you need to use the Directive.

    for example:

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

    Example: Plunker

提交回复
热议问题