Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directive doesn't work in a sub module

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'

like image 767
omeralper Avatar asked Jan 02 '17 21:01

omeralper


People also ask

Can directive be used as component?

A component is a single unit that encapsulates both view and logic whereas directives are used to enhance the behavior of components or dom elements and it doesn't have any templates. Component extends directive so every component is a directive.

How does a directive work in Angular?

In Angular, Directives are defined as classes that can add new behavior to the elements in the template or modify existing behavior. The purpose of Directives in Angular is to maneuver the DOM, be it by adding new elements to DOM or removing elements and even changing the appearance of the DOM elements.

Can a directive have templates?

Components are directives with templates. The only difference between Components and the other two types of directives is the Template. Attribute and Structural Directives don't have Templates. So, we can say that the Component is a cleaner version of the Directive with a template, which is easier to use.

What would you not put shared module?

What would you not put shared module? I would not put services in a shared module which may be imported by a lazy loaded module. When a lazy loaded module imports a module which provide a service, angular will create another instance of this service which may result in unexpected behaviors.


2 Answers

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.

like image 77
Stefan Svrkota Avatar answered Sep 26 '22 16:09

Stefan Svrkota


I faced with similar problem in our project. I had shared.module but it still threw an error

Can't bind to 'pathForUploading' since it isn't a known property of 'div'

I had this code

<div class="avatar"
     fileUploader
     [pathForUploading]="'path'"
     (onFileUploaded)="updateAvatar()"></div>

And it's gone after changing attribute calling to

pathForUploading="path"

Hope it will help somebody

like image 43
Aliaksei Avatar answered Sep 22 '22 16:09

Aliaksei