2017-02-09 6 views
6

지시문이있어서 여러 모듈에서 사용하고 싶습니다. 각 모듈에서이를 선언하면 오류가 발생합니다. 따라서 공유 모듈에서이 모듈을 선언하고이 공유 모듈을 다른 모듈로 가져 오려고했습니다. 그러나 작동하지 않았습니다. 구성 요소 자체의 모듈에서 정확하게 선언 된 경우에만 작동합니다.Angular2 - 동일한 모듈에서 선언되지 않은 경우 지시문이 작동하지 않습니다.

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 : 여기

SharedModule입니다 MyComponent

export class MyComponent implements OnInit, AfterViewInit { 

    @ViewChild(ModalDirective) modal: ModalDirective; 

    ... 
} 

modal는입니다 :

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 구성 요소에서 (ngAfterViewInit에있는 경우) ModalDirectiveMyModule에 선언되지 않은 경우누구든지이 문제를 해결하는 방법을 알고 있습니까? 에서

답변

10

당신의 SharedModule 당신이 ModalDirective

... 

@NgModule{(
    ... 
    exports: [ModalDirective] 
)} 
export class SharedModule { ... } 

이 근무 대한 추가 정보를 원하시면

+0

에 대해 공유 모듈에 docs를 참조 export해야합니다. 고맙습니다! –

+0

Thx 프레드릭! 내 지시어에는'TemplateRef'가 삽입되어 있습니다. 그러나 지시문을'NgModule.exports' 목록에 추가하면'No TemplateRef! '공급자가 없다는 오류가 발생합니다. 어떤 아이디어? – cosmozhang

+0

@cosmozhang - 코드를 보지 않고도 알 수는 없지만이 스레드를 살펴보십시오. https://stackoverflow.com/questions/35932074/angular2-no-provider-for-templateref-ngif-templateref –