Template parse errors: Can't bind to DIRECTIVE since it isn't a known property of 'div'

て烟熏妆下的殇ゞ 提交于 2019-12-04 17:24:43

An Angular module defines the template resolution environment for the template associated with every declared component. That means that when a component's template is parsed, it looks to THAT component's Angular module to find all of the referenced components, directives, and pipes.

A more common practice for something like this is to add the appListHighlight to a Shared module and then import that Shared module into your network module.

I have a youtube video about these concepts here: https://www.youtube.com/watch?v=ntJ-P-Cvo7o&t=6s

Or you can read more about this here: https://angular.io/guide/ngmodule-faq

In the picture below, I do something similar with the StarComponent, which is a nested component that turns a number into rating stars. You can use this same technique for your directive.

The error you're receiving is because the component is out of scope of the import location. The reason it works in your submodule would be because the component is declared in that submodule. The reason it doesn't work in other modules is because the component or directive aren't declared in the module.

Why?

At run time Angular will look at each module in a case by case basis. If the directive is declared in a sub-module it will check against components declared in that module and use the directive for them. If the directive is declared in your app.module it will only check against components directly declared in your app.module.

Solution?

The general solution is that you should declare a directive in each module where you declare a component that uses it. The other option is to add the directive into a shared module, and use the shared module in every other module where a component uses the directive.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!