如何为所有模块全局声明指令? [英] How to declare a directive globally for all modules?

查看:67
本文介绍了如何为所有模块全局声明指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Github仓库,该仓库遵循Angular(英雄之旅)的官方教程.您可以在此处看到所有代码.

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

我的问题是,我在应用程序的主模块(app.module)中声明了一个指令,并且如果在AppComponent中使用它,它会很好地工作(该指令仅突出显示DOM内的文本元素).

My problem, is that I have a directive declared in the main module of the app (app.module) and, if I use it inside the AppComponent, it works good (the directive only highlight a text inside a DOM element).

但是我在AppModule中有另一个名为HeroesModule的模块,并且在该模块的组件内部,该指令不起作用.

But I have another module called HeroesModule within AppModule, and inside a component of this module, this directive doesn't work.

主要代码,在这里:

app/app.module.ts

...

import { HighlightDirective } from "./shared/highlight.directive";

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        InMemoryWebApiModule.forRoot(InMemoryDataService),
        AppRoutingModule,
        CoreModule,
        HeroesModule
    ],
    declarations: [
        AppComponent,
        HeroTopComponent,
        HighlightDirective <-------
    ],
    providers: [
        { provide: APP_CONFIG, useValue: AppConfig }
    ],
    bootstrap: [ AppComponent ]
})

...

app/heroes/heroes.module.ts

@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        HeroRoutingModule
    ],
    declarations: [
        HeroListComponent,
        HeroSearchComponent,
        HeroDetailComponent,
        HeroFormComponent
    ],
    providers: [
        HeroService
    ],
    exports: [
        HeroSearchComponent
    ]
})

app/shared/highlight.directive.ts

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({ selector: '[tohHighlight]' })

export class HighlightDirective {
    constructor(el: ElementRef) {
        el.nativeElement.style.backgroundColor = 'yellow';
    }
}

app/app.component.ts

<h1 tohHighlight>{{title}}</h1> <----- HERE WORKS
<toh-nav></toh-nav>
<router-outlet></router-outlet>

app/heroes/hero-list/hero-list.component.ts

<div *ngIf="selectedHero">
    <h2>
        {{selectedHero.name | uppercase}} is my hero
    </h2>
    <p tohHighlight>Test</p> <----- HERE IT DOESN'T
    <button (click)="gotoDetail()">View Details</button>
</div>

如果需要,您可以在Github存储库中查看,安装并自己进行测试.

You can see, install it and test it by yourself in the Github repo, if you need it.

推荐答案

如果需要使用指令

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

每个地方都应创建一个新模块.

everywhere you should create a new Module.

如果使用Angular CLI,则可以生成:

If you use the Angular CLI you can generate:

ng g module my-common

模块:

@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.

例如:

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

示例:柱塞

这篇关于如何为所有模块全局声明指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆