Angularjs使用$ inject不起作用

时间:2023-01-19 21:48:02

I started a huge change in my AngularJs code to improve it. I was looking this reference and found it pretty interesting, so i was changing my code to follow thos guides.

我开始对我的AngularJs代码进行了大量更改以改进它。我正在寻找这个参考,并发现它非常有趣,所以我改变我的代码,以遵循thos指南。

When i got to the rout configuration i couldn't make it work. If i use a .config like the code below, everything works fine.

当我到达溃败配置时,我无法使其工作。如果我像下面的代码一样使用.config,一切正常。

angular
.module('agApp')
.config(
['$stateProvider', '$urlRouterProvider', '$locationProvider', 
function ($stateProvider, $urlRouterProvider, $locationProvider) {

    $urlRouterProvider.when("/", "/First");
    $urlRouterProvider.when("", "/First");
    $urlRouterProvider.otherwise("/First");

    $stateProvider
        .state('first', {
            url: "/First",
            views: {
                "main": {
                    templateUrl: "app/component/first.html",
                    controller: 'FirstController',
                    controllerAs: 'vm'
                }
            }
        })
        .state('second', {
            url: "/Second",
            views: {
                "main": {
                    controller: 'SecondController',
                    controllerAs: 'vm',
                    templateUrl: "app/component/second.html"
                }
            }
        })
}]);

But when i try to use the code as recomended on the guide, like this:

但是,当我尝试使用指南中推荐的代码时,如下所示:

angular
.module('agApp')
.config('ConfigRouter', ConfigRouter);

/* @ngInject */
function ConfigRouter($locationProvider,$stateProvider,$urlRouterProvider) {

    $urlRouterProvider.when("/", "/First");
    $urlRouterProvider.when("", "/First");
    $urlRouterProvider.otherwise("/First");

    $stateProvider
        .state('first', {
            url: "/First",
            views: {
                "main": {
                    templateUrl: "app/component/first.html",
                    controller: 'FirstController',
                    controllerAs: 'vm'
                }
            }
        })
        .state('second', {
            url: "/Second",
            views: {
                "main": {
                    controller: 'SecondController',
                    controllerAs: 'vm',
                    templateUrl: "app/component/second.html"
                }
            }
        })
}

Then it stops working and i keep getting this error:

然后它停止工作,我不断收到此错误:

Failed to instantiate module agApp due to: Error: [ng:areq]
Argument 'fn' is not a function, got string

由于以下原因无法实例化模块agApp:错误:[ng:areq]参数'fn'不是函数,得到字符串

I toke care of the proper injection (at least, I think I did) because the output minified code looks like this:

我关注正确的注入(至少,我认为我做了),因为输出缩小的代码如下所示:

function ConfigRouter(a,b,c){c.when( [.... rest of the code...]  ),ConfigRouter.$inject=["$locationProvider","$stateProvider","$urlRouterProvider"],

On the uglify process I'm using ngAnnotate in this order: concat, ngAnnotate and finaly uglify

在uglify过程中,我按此顺序使用ngAnnotate:concat,ngAnnotate和finaly uglify

Am I missing something here? What may be happening to my code?
Do I need any extra file to enable $inject?

我在这里错过了什么吗?我的代码可能会发生什么?我是否需要任何额外的文件来启用$ inject?

I'm trying to structure my AngularJs this way for the first time. By the way, the functionality of the controllers is not affect (also because there is no injection, just a simple function and name definitions), when i can run the app, the rest is working, the problem starts when I try to use the $inject.

我正试图第一次以这种方式构建我的AngularJs。顺便说一句,控制器的功能没有影响(也因为没有注入,只是一个简单的功能和名称定义),当我可以运行应用程序,其余的工作,当我尝试使用时问题开始$注入。

1 个解决方案

#1


4  

The error says it all: you're providing a string as an argument while it should be a function. That's because config blocks don't have names. It has to be

错误说明了一切:你提供一个字符串作为参数,而它应该是一个函数。那是因为配置块没有名称。它一定要是

angular
.module('agApp')
.config(ConfigRouter);

#1


4  

The error says it all: you're providing a string as an argument while it should be a function. That's because config blocks don't have names. It has to be

错误说明了一切:你提供一个字符串作为参数,而它应该是一个函数。那是因为配置块没有名称。它一定要是

angular
.module('agApp')
.config(ConfigRouter);