I can't make the directive work in a lazy loaded module. I've read the documentation and I simply added the directive into declarations array of my main module. The directive works as expected at that module, but it doesn't work in lazy loaded modules. It even prevents lazy loaded module to be opened because of the template error:

Can't bind to 'myHighlight' since it isn't a known property of 'p'

Here's my Plunker.

Check out errors in console after clicking 'go to child'

share|improve this question
    
It is mentioned in documentation, but I don't understand as in main module, directive is well declared... angular.io/docs/ts/latest/guide/… – Jacques Cornat Jan 2 at 22:11
    
Clarify the problem, or may be it's another problem? When you post a link to the code make sure the problem code is accompanied the question. – Roman C Jan 3 at 0:03
    
@RomanC Sorry for poor explanation. I've edited the question. – omeralper Jan 3 at 0:27
up vote 5 down vote accepted

That's because your directive is declared in AppModuleand it's only available there. If you want to use directive in both modules, you can create SharedModule and then declare and export directive from there, and then import SharedModule in your AppModule and your ChildModule:

@NgModule({
  declarations: [ HighlightDirective ],
  exports: [ HighlightDirective ]
})

export class SharedModule {}

Now you just need to add SharedModule to AppModule's and ChildModule's imports.

Note:

You have to remove your directive from AppModule's declarations since it can only be declared once.

share|improve this answer
    
But shouldn't the declarations declared in the AppModule be available globally throughout the app? – echonax Jan 3 at 5:40
1  
@echonax No, the only way to make anything globally available is the one I described, at least at this moment. – Stefan Svrkota Jan 3 at 10:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.