-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Creating a module twice fails silently #1779
Comments
This is by design - you should be able to replace a module with a new one, This is one reason why I tend to use exactly one module to one file. I agree though that this is an easy mistake to make and perhaps there
|
+1 i was having this problem and i didnt know why :) |
+1 for a more explicit api |
@amirhyoussefi you'd be better off creating a module in one file, say foo.js and adding things to it in other files. Use foo.js angular.module('foo', []); bar.js angular.module('foo').directive('bar', ...); baz.js angular.module('foo').filter('baz', ...); This pattern also has the benefit of not needing to introduce another global variable for your module. Yay. |
If we can provide a warning when it happens that would be great. An IDE can flag this (to say the least). @nah I had the same problem as the issue was hard to debug. |
Why not simply add to the dependencies? I.e. you would have: angular.module("foo", null, [some new deps]) and use null as a flag to add to the deps instead of replacing. The way it is now it's really a bitch to find out where the problem is when you suddenly get a blank page and nothing works |
@GEDDesign but the downside is that you need to remember to init your module in advance of calling angular.module('foo'). You won't really run into an error, but things won't work when you test. Another downside is that you can't add to the dependencies when you actually declare them, ie. when you have a larger module, you might want to define your dependency with your service and not with the module: angular.module("foo",["stuff this service depends on"]).service(...) so it will get added to later on. |
I run into this problem and could'n understand what i did wrong, there were no error and app just stopped working because i override whole module together with routes. This is bad experience for people that starting with angular as information about possibility to override module is hidden in documentation in one of parameters description. If there is no plans to change API, then maybe warning could be written to console, because i think this will be common mistake for beginners. Also documentation of module function if wrong in "Returns" part: Returns because it returns new module only if you specify dependencies, in other case it returns existing module. |
As part of our effort to clean out old issues, this issue is being automatically closed since it has been inactivite for over two months. Please try the newest versions of Angular ( Thanks! |
It wastes me half a day to find this issue page. I could just get useless error messages like: 'unknown provider', or 'circular dependency'. You should really consider using new APIs like:
or provide a warning message in the console. BTW, I'm using 1.2.0-rc2. |
I agree
|
I find that there are two fundamental requirements that the current
I wrote a decorator for Looking at the API we could change the single-argument version of So for now I propose just adding a new API which makes it easy to append to the list of dependencies: var mod = angular.module('name');
mod.dependencies(['dep1', 'dep2']); I'll work on raising a pull request for this. |
@hiddentao if you split up everything into separate modules and import them into their direct parents then the current api works just fine and satisfies all problems. That is the way i went after trying to declare parts of modules in separate files. Just have separate modules in separate files, and import when needed. One module per file. |
@j-walker23 Yeah that would work. But I'd like to be able to do it the way I want as splitting a module into multiple files just intuitively makes sense to me; it's what I'm used to from other frameworks. I also stick to one module per file..I guess you meant to say one file per module? |
I understand, its hard to move away from a pattern that you like and are used to. The other reason i had to stop partial module declarations was my build script. To load a partial module the original declaration had to be imported first, and everything was imported alphabetical due to grunt reading the directory. If you have not, check out https://github.com/ngbp/ngbp. It helped me figure out a good module structure for a large app. Sorry, yeah i meant one file per module. |
I wrote a small proxy script in https://github.com/bahmutov/stop-angular-overrides. Install using |
How about making
|
+1 on last comment. Just spent half a day chasing this issue (and arriving at this page). |
@petebacondarwin, I hope you don't mind me reopening this issue for now. I think even though current behavior is needed for Karma we could have current behavior available only in Karma, via switching a particular config flag in |
Perhaps this is something that we can fix in 1.4? |
@petebacondarwin That would be great! |
@petebacondarwin Should we have a milestone for things we plan for |
We have the "1.4 candidate" label at the moment |
@WeHateNick @petebacondarwin you can include https://github.com/bahmutov/stop-angular-overrides script before your client code to overwrite module (and other calls) and stop silent behavior |
I'm going to be working on this for Angular 1.4 |
@pkozlowski-opensource in case of lazy loading files (with ocLazyLoad for example) you might want to call the same file twice to re-execute run/config blocks. Also if your module is generated server-side with a few key elements changing, you might want to redefine the modules (ie: when the user logs in). It depends on how you implement the "fix", could you maybe add an option to keep the current implementation (like the "debugInfoEnabled" option ?), it would throw the error by default but you could set a flag somewhere to say: ok I know it's risky but let me do it. Also if you could add at the same time an option to list the currently loaded modules & components (directives, services, ...), this would be really awesome for lazy loading libraries (I think @geddski would like that for Overmind as well). I know that you don't support lazy loading components in angular 1.x, but I don't think it would be that hard to do: the list exists internally, it's just a matter of making it available via a method (probably on the $injector, I could send a PR for that if you want). |
@ocombe what you are saying is true but it uses on-demand / lazy loading as a main argument. So IMO we should focus on making lazy-loading the right way. |
I agree, and it would be fantastic to be able to do that for angular 1.x, but it will be a lot of work. If you decide to work on a real lazy loading for angular, let me know I'd be glad to help you ! |
I've wasted maybe 4-5 hours trying to solve a generic I just assumed reopening would add to the module definition rather than replacing it. Clearly this was a mistake, but it's an easy mistake to make and it's difficult to determine the cause of your error. |
Yeh, so this is really confusing, we should be throwing an exception in case of redefining a module, instead of overriding things silently. 1.5 might be a good candidate for it if it doesn't get solved in 1.4 |
+1 ... it's super error prone and super frustrating when shit happens. A single flag would avoid wasting so much time in the world 😞 :
|
will cause the first directive registration to be lost.
The text was updated successfully, but these errors were encountered: