gpt4 book ai didi

javascript - AngularJS 服务未定义 : Unknown provider: $scopeProvider <- $scope

转载 作者:可可西里 更新时间:2023-11-01 02:17:44 24 4
gpt4 key购买 nike

我已经开始学习 Angular JS,但在将服务注入(inject) Controller 时遇到了问题。我试图将 ThreadFactory 服务放入 ThreadController 中,但在调用它时遇到未定义的错误。任何建议都会很棒。我得到的错误是:

未知提供者:$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";
}
}
}]);

进口顺序

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

最佳答案

您实际上无法按照您尝试的方式将 $scope 注入(inject)到您的 ThreadService 中。当您将 $scope 注入(inject) Controller 时,它不是典型的服务。如果您从 Threadservice.js 中删除 $scope 注入(inject),我敢打赌错误会消失。

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

Injecting $scope into an angular service function()

关于javascript - AngularJS 服务未定义 : Unknown provider: $scopeProvider <- $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26706649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com