Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have just started with AngularJS The following code gives an error in the console.

Unknown provider: $scopeProvider <- $scope <- newActiveOrdersModel . I have researched but it looks like Unknown Provider error can happen for a variety of reasons.It would be nice if anyone could point out where I am going wrong ?

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

It seems like Angular Js cannot recognize "newActiveOrdersModel".

like image 388
Abhik Avatar asked Oct 23 '13 15:10

Abhik


People also ask

What does $scope mean in AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

Why $scope is used in AngularJS?

Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the "AngularJS realm" (controllers, services, AngularJS event handlers). Scopes can be nested to limit access to the properties of application components while providing access to shared model properties.

What is scope and $scope in AngularJS?

AngularJS ScopeThe scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.

What is $error in AngularJS?

This error results from the $injector being unable to resolve a required dependency. To fix this, make sure the dependency is defined and spelled correctly. For example: angular. module('myApp', []) .


1 Answers

This just a guess but I don't know why you have $scope listed as a dependency of your service. I think something like this

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

will solve the error. Also I wouldn't include $rootScope unless you absolutely need it. It's generally considered bad practice in Angular to store stuff in the $rootScope.

like image 112
NicolasMoise Avatar answered Sep 17 '22 16:09

NicolasMoise