Globally register a directive in Angular

前端 未结 1 1429
南笙
南笙 2021-02-12 20:14

I am developing an Angular application. I need to add special behavior to all links. In AngularJS would just write a directive like this:

angular.module(\'whatev         


        
相关标签:
1条回答
  • 2021-02-12 20:56

    My understanding is that you have to opt in to all custom directives at the component level. Only PLATFORM_DIRECTIVES are implicitly included (ngFor, ngIf etc.).

    However, you can register your own custom directive as a PLATFORM_DIRECTIVE

    import { provide, PLATFORM_DIRECTIVES } from '@angular/core';
    
    bootstrap(RootCmp, [
      provide(PLATFORM_DIRECTIVES, {useValue: YourCustomDirective, multi: true}),
    ]);
    

    Here is an article that talks more about the process: http://blog.thoughtram.io/angular2/2015/11/23/multi-providers-in-angular-2.html

    EDIT: I consider this less of a concern now since components are declared at the module level. This means a lot less repetition since you no longer have to declare child components at the individual component level.

    0 讨论(0)
提交回复
热议问题