javascript AngularJS 服务未定义:未知提供者:$scopeProvider <- $scope

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26706649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:21:24  来源:igfitidea点击:

AngularJS Service Undefined: Unknown provider: $scopeProvider <- $scope

javascriptangularjs

提问by Ben Harris

I've started learning Angular JS and I'm having a problem with injecting a service into a controller. I'm trying to put the ThreadFactoryservice into ThreadController, but I'm getting an undefined error when calling it. Any advice would be great. The error I'm getting is:

我已经开始学习 Angular JS,但在将服务注入控制器时遇到了问题。我正在尝试将该ThreadFactory服务放入ThreadController,但在调用它时出现未定义的错误。任何建议都会很棒。我得到的错误是:

Unknown provider: $scopeProvider <- $scope <- ThreadService

未知提供者:$scopeProvider <- $scope <- ThreadService

app.js

应用程序.js

angular.module('threadsApp', ['ngRoute']);
angular.module('threadsApp')
    .config(function ($routeProvider, $locationProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'views/index.html',
            })
            .when('/selected/:topicName', {
                templateUrl: 'views/threads.html',
                controller: 'ThreadController',
            })
            .otherwise({
                redirectTo: "/"
            });
            $locationProvider.html5Mode(true);
    });

ThreadController.js

线程控制器.js

angular.module('threadsApp').controller("ThreadController",
    ["$scope", "$route", "$routeParams", "ThreadService", function ($scope, $route, $routeParams, ThreadService) {
    $scope.test = "Hello!";
    $scope.test2 = ThreadService.get();
}]);

ThreadService.js

线程服务.js

angular.module('threadsApp').service("ThreadService", ["$scope", function ($scope) {
    return {
        get: function() {
            return "Hello";
        }
    }
}]);

Order of Imports

进口顺序

    <script src="bower_components/jquery/dist/jquery.js"></script>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-route/angular-route.js"></script>
    <script src="bower_components/bootstrap/dist/js/bootstrap.js"></script>
    <script src="components/app.js"></script>
    <script src="components/bodyController.js"></script>
    <script src="components/TopicController.js"></script>
    <script src="components/ThreadService.js"></script>
    <script src="components/ThreadController.js"></script>

回答by jdotjdot

You can't actually inject $scopeinto your ThreadServicethe way you're trying to. $scopeisn't a typical service when you inject it into a controller. If you remove the $scopeinjection from Threadservice.js, I would bet the error will go away.

您实际上无法按照$scope自己ThreadService的方式注入。 $scope当您将其注入控制器时,它不是典型的服务。如果您$scopeThreadservice.js 中删除注入,我敢打赌错误会消失。

In the interest of not being redundant, a fuller explanation can be found here:

为了避免冗余,可以在此处找到更完整的解释:

Injecting $scope into an angular service function()

将 $scope 注入角度服务函数()