AngularJS服务未定义:未知提供商:$ scopeProvider< - $范围 [英] AngularJS Service Undefined: Unknown provider: $scopeProvider <- $scope

查看:134
本文介绍了AngularJS服务未定义:未知提供商:$ scopeProvider< - $范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习角JS和我在与注射服务为一体的控制器出现问题。我试图把的ThreadFactory 服务为 ThreadController ,但调用它时我发现了一个未定义的错误。任何意见将是巨大的。我得到的错误是:

未知提供商:$ scopeProvider< - $范围< - ThreadService

app.js

  angular.module('threadsApp',['ngRoute']);
angular.module('threadsApp')
    的.config(函数($ routeProvider,$ locationProvider){
        $ routeProvider
            。什么时候('/', {
                templateUrl:意见/ index.html的,
            })
            。当('/选择/:topicName',{
                templateUrl:意见/ threads.html',
                控制器:'ThreadController',
            })
            。除此以外({
                redirectTo:/
            });
            $ locationProvider.html5Mode(真);
    });

ThreadController.js

  angular.module('threadsApp')。控制器(ThreadController
    [$范围,$路线,$ routeParams,ThreadService功能($范围,$路径,$ routeParams,ThreadService){
    $ scope.test =你好!;
    $ scope.test2 = ThreadService.get();
}]);

ThreadService.js

  angular.module('threadsApp')。服务(ThreadService,[$范围功能($范围){
    返回{
        得到:函数(){
            返回你好;
        }
    }
}]);

进口订单

 <脚本SRC =bower_components / jQuery的/距离/ jquery.js和>< / SCRIPT>
    &所述; SCRIPT SRC =bower_components /角度/ angular.js>&下; /脚本>
    <脚本SRC =bower_components /角路由/角route.js>< / SCRIPT>
    <脚本SRC =bower_components /引导/距离/ JS / bootstrap.js>< / SCRIPT>
    <脚本SRC =组件/ app.js>< / SCRIPT>
    <脚本SRC =组件/ bodyController.js>< / SCRIPT>
    <脚本SRC =组件/ TopicController.js>< / SCRIPT>
    <脚本SRC =组件/ ThreadService.js>< / SCRIPT>
    <脚本SRC =组件/ ThreadController.js>< / SCRIPT>


解决方案

您不能真正注入 $范围 ThreadService 你想要的方式。 $范围不是一个典型的服务,当你把它注射到控制器。如果从删除 $范围注射的 Threadservice.js 的,我敢打赌错误会消失。

在不是冗余的利益,更全面的解释可以在这里找到:

<一个href=\"http://stackoverflow.com/questions/22898927/injecting-scope-into-an-angular-service-function\">Injecting $范围成多角服务功能()

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 ThreadFactory service into ThreadController, but I'm getting an undefined error when calling it. Any advice would be great. The error I'm getting is:

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

app.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

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

ThreadService.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>

解决方案

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

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

Injecting $scope into an angular service function()

这篇关于AngularJS服务未定义:未知提供商:$ scopeProvider&LT; - $范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆