6
我有一個指令,我想在多個模塊中使用它。在每個模塊上聲明它會產生錯誤。因此,我試圖在共享模塊中聲明它,並將此共享模塊導入其他模塊。但是,它沒有工作。它僅在組件自己的模塊上聲明時纔有效。Angular2 - 指令在未在同一模塊中聲明時不工作
這是我SharedModule
:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GeneralDataService } from './general-data.service';
import {ModalDirective} from '../directives/modal.directive';
@NgModule({
imports: [
CommonModule
],
providers: [GeneralDataService],
declarations: [ModalDirective]
})
export class SharedModule { }
而且我想要的模塊來使用ModalDirective
:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './patient.component';
import {SharedModule} from '../shared/shared.module';
@NgModule({
imports: [
CommonModule,
SharedModule
],
providers: [ ],
declarations: [ MyComponent ]
})
export class MyModule { }
在MyComponent
組件:
export class MyComponent implements OnInit, AfterViewInit {
@ViewChild(ModalDirective) modal: ModalDirective;
...
}
在MyComponent
modal
爲undefined
(即使在ngAfterViewInit
)如果ModalDirective
未在MyModule
中聲明。任何人都知道如何解決此問題?
。謝謝! –
Thx Fredrik!我的指令注入了'TemplateRef'。但是,當我將該指令添加到'NgModule.exports'列表中時,出現錯誤提示「沒有TemplateRef!提供程序」。有任何想法嗎? – cosmozhang
@cosmozhang - 無法看到您的代碼真的不知道,但看看這個線程:https://stackoverflow.com/questions/35932074/angular2-no-provider-for-templateref-ngif-templateref –