AngularJS - 服务未在app.js中注册

问题描述 投票:0回答:1

我过去做过这个,但这次它一直在失败,不知道我哪里出错了。以下是我的档案

app js

(function () {

    var app = angular.module("ScmCodeViewer", ["ngRoute"]);

    app.config(function ($routeProvider) {
        $routeProvider
            .when("/gitlab", {
                templateUrl: "gitlab.html",
                controller: "gitlabController"
            });
    });

}());

服务js

(function () {

    var app = angular.module("ScmCodeViewer");

    app.factory("gitlabService", function ($http) {
        ....
        }
        return {
            getFiles: getFiles
        }
    });

});

当我尝试在我的控制器中使用gitlabService时,如下所示,我收到“未知提供程序”错误

(function () {

    var app = angular.module("ScmCodeViewer");
    app.controller("gitlabController", function ($scope, $log, gitlabService) {


            gitlabService.someMethod();
        }
    });

}());

但是,如果我将服务js中的代码移动到app js并在那里注册服务,它似乎工作正常。

HTML

<head>
    <script src="scripts/external/angular.min.js"></script>
    <script src="scripts/external/angular-route.min.js"></script>

    <script src="scripts/app.js"></script>
    <script src="scripts/gitLabService.js"></script>
    <script src="scripts/gitlabController.js"></script>
</head>
angularjs angular-services
1个回答
0
投票

你忘了执行你的服务js。你需要最后的();来确保函数运行。

(function () {

    var app = angular.module("ScmCodeViewer");

    app.factory("gitlabService", function ($http) {
        ....
        }
        return {
            getFiles: getFiles
        }
    });

})(); //right here you need the ()
© www.soinside.com 2019 - 2024. All rights reserved.