最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Configuration annotations error: Argument 'fn' is not a function, got string - Stack Overflow

programmeradmin4浏览0评论

I've used annotations everywhere (in controllers, services,...) except for a few files. As I'm getting problems with minification, I'm rewriting these last few files.

This is my current config:

(function () {

    angular.module('app')
        .config(function ($stateProvider, $urlRouterProvider, $provide, $httpProvider, showErrorsConfigProvider, LightboxProvider) {
            $httpProvider.interceptors.push('requestsErrorHandler');
            $urlRouterProvider.otherwise('/');
            showErrorsConfigProvider.showSuccess(true);

            // more code
            // routing and stuff...


        });

}());

This works fine. I'm now trying to rewrite this with annotations:

(function () {

    angular.module('app')
        .config('configurationapp', configurationapp);

    configurationapp.$inject = ['$stateProvider', '$urlRouterProvider', '$provide', '$httpProvider', 'showErrorsConfigProvider', 'LightboxProvider'];

    function configurationapp($stateProvider, $urlRouterProvider, $provide, $httpProvider, showErrorsConfigProvider, LightboxProvider) {
            $httpProvider.interceptors.push('requestsErrorHandler');
            $urlRouterProvider.otherwise('/');
            showErrorsConfigProvider.showSuccess(true);


            // more code
            // routing and stuff


        }
}());

However, I'm getting this error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [ng:areq] Argument 'fn' is not a function, got string

I don't understand why I'm getting this error. I've done exactly the same as with all other files, still this doesn't work.

What am I doing wrong?

I've used annotations everywhere (in controllers, services,...) except for a few files. As I'm getting problems with minification, I'm rewriting these last few files.

This is my current config:

(function () {

    angular.module('app')
        .config(function ($stateProvider, $urlRouterProvider, $provide, $httpProvider, showErrorsConfigProvider, LightboxProvider) {
            $httpProvider.interceptors.push('requestsErrorHandler');
            $urlRouterProvider.otherwise('/');
            showErrorsConfigProvider.showSuccess(true);

            // more code
            // routing and stuff...


        });

}());

This works fine. I'm now trying to rewrite this with annotations:

(function () {

    angular.module('app')
        .config('configurationapp', configurationapp);

    configurationapp.$inject = ['$stateProvider', '$urlRouterProvider', '$provide', '$httpProvider', 'showErrorsConfigProvider', 'LightboxProvider'];

    function configurationapp($stateProvider, $urlRouterProvider, $provide, $httpProvider, showErrorsConfigProvider, LightboxProvider) {
            $httpProvider.interceptors.push('requestsErrorHandler');
            $urlRouterProvider.otherwise('/');
            showErrorsConfigProvider.showSuccess(true);


            // more code
            // routing and stuff


        }
}());

However, I'm getting this error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [ng:areq] Argument 'fn' is not a function, got string

I don't understand why I'm getting this error. I've done exactly the same as with all other files, still this doesn't work.

What am I doing wrong?

Share asked Oct 27, 2015 at 8:58 Bv202Bv202 4,07415 gold badges49 silver badges81 bronze badges 2
  • May be some module is failing to load. Please have a look at your browser's console to see if any files are not being downloaded/loaded correctly. – Khalid Hussain Commented Oct 27, 2015 at 9:02
  • Nope, everything seems to be fine. I'm sure the problem is related to this code, as when I revert the changes, everything works again. – Bv202 Commented Oct 27, 2015 at 9:05
Add a ment  | 

2 Answers 2

Reset to default 6

A string was provided to config:

angular.module('app')
    .config('configurationapp', configurationapp);

It expects a single argument, function or array. configurationapp isn't Angular service and shouldn't be named:

angular.module('app')
    .config(configurationapp);

Try this syntax. I find the inline annotation easier than the explicitly inject dependencies:

  (function () {

    angular.module('app')
        .config(['$stateProvider', '$urlRouterProvider', '$provide', '$httpProvider', 'showErrorsConfigProvider', 'LightboxProvider',
        function ($stateProvider, $urlRouterProvider, $provide, $httpProvider, showErrorsConfigProvider, LightboxProvider) {
            $httpProvider.interceptors.push('requestsErrorHandler');
            $urlRouterProvider.otherwise('/');
            showErrorsConfigProvider.showSuccess(true);

            // more code
            // routing and stuff...


        }]);

}());

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论