Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 directive is not initialized when placed in lazy loaded module's component

Very new to Angular 6.

I have declared a directive in AppModule and in the appComponent template if I apply that directive it works, In the app component there is a router outlet in which a lazy loaded module gets loaded at very beginning.

Now if I want to apply the directive in any component template of the lazyloaded module, the directive is not being initialized. The directive is an attribute directive and I have declared it properly and used too because inside app component it's working fine.

Not getting any clue, please help.

like image 274
Abhisek Malakar Avatar asked Jun 27 '18 17:06

Abhisek Malakar


1 Answers

I see there is an issue with your design. Instead of having your directive in the AppModule, create a SharedModule and then implement the directive in there. Import SharedModule everywhere else in your app. This way you can access your directive from external components (from other modules).

Make sure you declare and export it inside SharedModule

I thought if anything declared in app module should be available to all modules

No this is not correct, Child modules does not know what you have inside the AppModule. ChildModule's are just another independent modules like AppModule. You could have simply exposed AppModule to ChildModule, but that might cause the circular dependency. That is the reason you have to make use of SharedModule

like image 161
Amit Chigadani Avatar answered Nov 23 '22 17:11

Amit Chigadani