AngularJS在尝试使用模块时抛出未知提供程序:$scopeProvider <-$scope错误

AngularJS throws Unknown provider: $scopeProvider <- $scope error when I try to use modules

本文关键字:scopeProvider 程序 错误 scope 模块 未知 AngularJS      更新时间:2023-09-26

我刚刚开始使用AngularJS。以下代码在控制台中给出错误。

未知提供程序:$scopeProvider <- $scope <- 新活动订单模型。我已经进行了研究,但看起来未知提供程序错误可能由于各种原因而发生。如果有人能指出我哪里出错了,那就太好了?

var app;
(function(angular){
    app = angular.module('OrdersDashboard',[]);
    app.config(['$routeProvider', function($routeProvider) {
        $routeProvider.
            when('/current/new', {templateUrl: 'orders/partials/new_current', controller: 'newActiveOrdersCtrl'}).
            otherwise({redirectTo: '/'});
    }]);
    app.service('newActiveOrdersModel', ['$scope', '$rootScope',
        function($scope, $rootScope){
            this.Orders=["This is a test order"];
            this.fetchOrders = function(){
                console.log("This is a test order");
                this.Orders=["This is a test order1111"];
            };
        }]);
    app.controller('newActiveOrdersCtrl', ['$scope', '$rootScope', 'newActiveOrdersModel',
        function($scope, $rootScope, newActiveOrdersModel){
            $scope.test="Hello World";
        }]);
})(angular);

似乎Angular Js无法识别"newActiveOrdersModel"。

这只是

一个猜测,但我不知道为什么你$scope列为服务的依赖项。我认为像这样的事情

 app.service('newActiveOrdersModel', ['$rootScope',
    function($rootScope){..}]

将解决错误。此外,除非您绝对需要,否则我不会包括$rootScope。在 Angular 中,将东西存储在$rootScope中通常被认为是不好的做法。