2016-11-23 45 views
0

我有我需要在整个应用程序中使用一些自定义的指令,在全球范围内注册一个指令。如何注册我的指令以在整个应用程序中使用?我遇到的其他问题都过时了。我需要角2最终版本的解决方案2.1.0在angular2最终

main.ts看起来像这样

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 
import { enableProdMode } from '@angular/core'; 
import { AppModule } from './app.module'; 

//enableProdMode(); //Uncomment for production 
platformBrowserDynamic().bootstrapModule(AppModule) 
    .then((success: any) => console.log('App bootstrapped')) 
    .catch((err: any) => console.error(err)); 

我的应用程序有6个模块使用延迟oading

appmodule 
--contact module 
--project module 
--restmodules 
... 

当我注册的产品指令模块,它运行良好。所以,我在模块的休息,我注册了这个指令在appmodule.ts这样

import { mdinput} from './shared/mdinput.directive'; 


@NgModule({ 
    imports: [ 
       AppRoutingModule 

    ], 
    declarations: [ 
     AppComponent,mdinput 

    ], 
    providers: [DataService], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

现在这个特定的指令无法正常工作使用此指令。它也没有给出任何错误。

+0

只需将它添加到您的NgModule的声明。 https://angular.io/docs/ts/latest/guide/attribute-directives.html –

+0

@JBNizet但它不工作的模块不同势 –

+0

不要让我们猜你想达到的目标。编辑您的问题,并张贴关于你的指令,你的模块,等等。你可能需要将其添加到出口,太,但没有看到任何东西的所有相关信息,这是很难猜测。 –

回答

1

只需将其添加到功能模块和功能模块添加到您要使用它的模块的imports: [...]

即使*ngIf*ngFor,...现在需要通过增加CommonModuleBrowserModuleimports加入这种方式。

欲了解更多详情,请参阅https://angular.io/docs/ts/latest/guide/ngmodule.html

2

首先,在一个单独的目录中创建指令。假设您在customDirectives目录中创建了指令:myHighlight.directive.ts。现在

import { HighlightDirective } from 'yourpath/customDirectives/myHighlight.directive'; 

declarations: [..., HighlightDirective] 

app.module.ts添加这些。

现在,在任何组件视图中使用:

<td myHighlight>{{user.name | capitalize}}</td> 

这将如何工作。