Search code examples
angularjsangular-bootstrap

Angular itkn error caused by directives defined with function outside array


I'm using Angular 1.3.15, also have tried 1.2.28, along with angular bootstrap. Last night, after updating Chrome to v42.0.2311.90, I suddenly began seeing tons of angular itkn errors:

Error: [$injector:itkn] Incorrect injection token! Expected service name as string, got function ()
http://errors.angularjs.org/1.3.15/$injector/itkn?p0=function%20()
    at REGEX_STRING_REGEXP (angular.min.js?bust=undefined:63)
    at Object.invoke (angular.min.js?bust=undefined:4189)
    at angular.min.js?bust=undefined:6525
    at forEach (angular.min.js?bust=undefined:323)
    at Object.<anonymous> (angular.min.js?bust=undefined:6523)
    at Object.invoke (angular.min.js?bust=undefined:4204)
    at Object.enforcedReturnValue [as $get] (angular.min.js?bust=undefined:4056)
    at Object.invoke (angular.min.js?bust=undefined:4204)
    at angular.min.js?bust=undefined:4021
    at Object.getService [as get] (angular.min.js?bust=undefined:4162)

This wasn't happening before updating Chrome but I guess I can't say that's for sure related.

After poking through, I've found that any directives defined like this:

  .directive('modalTransclude', function () {
    return {
      // directive code
    };
  })

Will cause this itkn error. I console.logged the key in the $inject iterator in angular and it's returning the function of the directive itself rather than only strings like was clearly intended.

I've discovered that wrapping the function in an array, even without any strings defining the injections, it solves the issue:

  .directive('modalTransclude', [function () {
    return {
      // directive code
    };
  }])

However, this error is not happening on the angular bootstrap site, and I've tried this on both angular 1.3 and 1.2 versions.

I'll continue looking for any causes that are unique to my app, but does anyone have any idea what's causing this?


Solution

  • If you have ng-inspector.org extension installed in chrome, it might be a reason. Just switch off ng-inspector browser extension.

    I don't know exact reasons why it's not working with ng-inspector at the moment. If someone have time to look into this, it would be nice to submit bug to project involved (I suspect it's ng-inspector itself).