Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directives cannot be used in more than one module #10982

Closed
lcurrens opened this issue Aug 22, 2016 · 3 comments
Closed

Directives cannot be used in more than one module #10982

lcurrens opened this issue Aug 22, 2016 · 3 comments

Comments

@lcurrens
Copy link

I'm submitting a ... (check one with "x")

[X ] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
Directives cannot be used in more than one module. For example, in main NgModule:
,
import {MyDirective} from './myDirective';

@NgModule({
imports: [
...
],
declarations: [
MyDirective
],

I need to use MyDirective in another module, as it provides general purpose functionality.

If I declare the directive in the other module, I get an error:
Error: Type MyDirective is part of the declarations of 2 modules: Module1 and Module2!

On the other hand, if I just try to use the directive without declaring it in the second module, the directive is undefined.

Expected/desired behavior
There is no way to define a Directive at the top level Module and have lower level modules use that same directive. If you declare it in both modules there is an error. If you don't declare it in the second module it is undefined.

Reproduction of the problem
If the current behavior is a bug or you can illustrate your feature request better with an example, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (you can use this template as a starting point: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5).

What is the expected behavior?

Allow directive to be shared between modules.

What is the motivation / use case for changing the behavior?
I have a general purpose directive, need to be able to use it in more than one module.

Please tell us about your environment:
windows

  • Angular version: 2.0.0-rc.X
    RC5
  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

Chrome

  • Language: [all | TypeScript X.X | ES6/7 | ES5]
    Typescript
@crain
Copy link

crain commented Aug 22, 2016

You could declare your directive in a seperate module and import this module in your other modules where you need it:

@NgModule({
    declarations: [
        MyDirective
    ],

    exports: [
        MyDirective
    ]
})
export default class MyModule{}

@qdouble
Copy link

qdouble commented Aug 22, 2016

Yeah, this isn't a bug, you are supposed to declare it in one module and just share that module: https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants