当我尝试使用模块时,AngularJS抛出未知提供者:$ scopeProvider < - $ scope错误

Abh*_*hik 16 javascript angularjs

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

未知提供者:$ scopeProvider < - $ scope < - newActiveOrdersModel.我已经研究过,但看起来像Unknown Provider错误可能由于各种原因而发生.如果有人能指出我哪里出错了会很好吗?

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);
Run Code Online (Sandbox Code Playgroud)

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

Nic*_*ise 34

这只是一个猜测,但我不知道你为什么将$ scope作为服务的依赖项列出.我觉得这样的事情

 app.service('newActiveOrdersModel', ['$rootScope',
    function($rootScope){..}]
Run Code Online (Sandbox Code Playgroud)

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

  • 好的,有时您只需要使用rootScope,但我建议您将事件存储在另一个服务中,而不是使用全局范围.同样,使用全局范围没有任何本质上的错误,但为了防止命名冲突和过度填充全局范围,它被认为是更好的做法. (2认同)