5 ms·
You don't need to worry about dependency injection breaking when your code is minified. You can supply a list of the dependencies as strings, and minified code
by ahuth 13y ago
You don't need to worry about dependency injection breaking when your code is minified. You can supply a list of the dependencies as strings, and minified code will work fine:
angular.module("whatever").controller("YourController",
["dep1", "dep2", function (dep1, dep2) {
whatever...
}]);
Even though the `dep1` and `dep2` params get minified, angular will still pass in the correct dependencies.
- lerouxb 13y agoShouldn't that just be the only and correct way, then? No need for .toString() magic..
- ahuth 13y agoSure, that makes sense to me. Looks like I misunderstood the author's intent. This should be the only way, though. Also, I didn't even realize that this way doesn't require .toString magic.
- lerouxb 13y agoActually I'm not 100% sure it that it doesn't require toString magic at all, but it would at least require less and probably be more reliable.
- aidos 13y agotoString is only used to parse the function signature to get the list of dependencies. If you use the other syntax it doesn't have to do the "magic" at all. I haven't looked at the output (though the magic function itself is pretty simple) but I would imagine it just creates the explicit version that you can choose to write manually. I'm a little torn myself; on the one hand, I prefer explicit code without magic. Having said that, you often need quite a few dependencies and it's a pain, and ugly, to write them all out twice on all your signatures.
- ahuth 13y agoAgreed. And at least it's more clear that dependencies are being injected.
- Cthulhu_ 13y agoThen again, reading about Angular's DI for a few minutes should lock this convention into your mind pretty quickly, I think. An array of strings where the last entry is a function that takes arguments named after the strings of the array it's defined in... sounds rather hacky and confusing to me, not to mention it duplicates strings. Easy to make a typo, easy to change the order of arguments - and no compiler or any kind of sensible IDE or tool to warn you about it.
- AdrianRossouw 13y agoit doesn't need the toString this way, because it's already a string being passed in.
- AdrianRossouw 13y agothat's exactly my point why have the magical version if all it does is break, and there's a perfectly valid way to do it that is really not THAT much more complex. The magic version is the first thing they teach you, and then you have to read the fine print to see you shouldn't be using it. Why just not have it in the first place?
- jonrimmer 13y agoBecause writing controllers, services, directives, etc. with various dependencies is something you do all the time, and the DI reflection lets you do so in a quicker, friendlier, and non-duplicated way, whereas setting up the ng-min step is something you only have to do once, then you can forget about it. You already need to set up a build pipeline to get your minification, concatenation, etc. in the first place. It's hardly a terrible burden to have to stick an ng-min step in there as well. It seems like you're trying to justify an instinctive distaste for this feature by exaggerating how problematic it is, when in reality it isn't problematic at all.
- ahuth 13y agoGotcha. Your point is that this way (that doesn't break when minified) should be the only way to do it, right? I'll agree with that. Initially, I thought your point was the dependency injection as a concept needs to go away in Angular.
- AdrianRossouw 13y agogod no. the dependency injection is fine. i just think that the unsafe way shouldn't even exist, much less be the first and most prominently featured in the documentation. It even has fine print, which everybody has to add in everywhere whenever they talk about it.
- leddt 13y agoFor what it's worth, I use ng-min which will automatically generate the safe syntax when minifying. I like it very much because writing the safe syntax does not feel "dry" enough to me.