Not able to annotate directive controller dependency injections
See original GitHub issue(function() {
'use strict';
angular
.module('app.widgets')
.directive('directiveName', directiveName);
function directiveName(someService) { //This is working
return {
controller: function(anotherService) { //No annotations generated for this one
}
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
How to inject dependencies in directive controller in following ...
Try separating the directive and it's controller into two separate files. Then you can do injection like you normally would, in the controller....
Read more >Developer Guide: Dependency Injection - AngularJS: API
Dependency Annotation. AngularJS invokes certain functions (like service factories and controllers) via the injector. You need to annotate these functions so ...
Read more >Avoid AngularJS Dependency Annotation with Rails
In AngularJS, it is a common practice to annotate injected dependencies for controllers, services, directives, etc. For example:.
Read more >Use explicit dependency injection annotation in directives with ...
Bad practice (no explicit annotation used for injecting dependencies into a directive used in an application bootstrapped in strict DI mode). angular
Read more >Directive Controllers Can Use Dependency Injection In ...
Yeah, if the injected values need to be exposed to the Link and/or Compile functions (or any other function inside the directive factory)....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just to necro this issue again, I’m here also because of this slight discrepancy between ng-annotate behaviour and the John Papa style guide, too.
Because I wasted 45 minutes on this, here’s a recap for anyone else (and google):
The Angular exception raised takes this form:
Because the minifier has minified the arguments on the Controller function, but ng-annotate couldn’t identify the Directive’s controller as a controller and thus failed to generate the
$inject
call.The difference, as pointed out by @olov, is in returning the directive definition rather than setting it on an object and then returning:
Does Not Work
Does Work
It’s a minor annoyance with a simple workaround, but as the Angular errors for minified controller arguments are so completely useless (and this is why we use ng-annotate) it’s quite the time waster when you bump into it.
Citing @bartvanderwal comment about John Papa’s guide being endorsed by the Angular team, it would be great if something could be done to support this in the future.
This was confirmed using
gulp-ng-annotate 2.0.0
withng-annotate 1.2.1
under the hood.Thanks, I changed it and it worked. Could this be supported? As this form is recommended both in https://github.com/johnpapa/angular-styleguide#directives and in https://docs.angularjs.org/api/ng/service/$compile