GonnaHack
GonnaHack

Reputation: 77

$rootscope throwing error in angular Js

I'm practicing $rootscope in Angular JS. Here I'm defining rootscope as below

var sample=angular.module("sample",[]).run(['$rootscope', function($rootScope){
            $rootScope.months=12;
        }]) 

And this rootscope calling in inner scope as below

sample.controller('salary', ['$scope','$rootscope', function($rootScope,$scope){
            $scope.dept="developer";
            $scope.salary=58900
            $scope.getAnualSalary=function () {
                return (this.salary)*$rootScope.months;
            }
        }])

but I doesn't showing desired result and throwing error in console

angular.js:14800 Error: [$injector:unpr] http://errors.angularjs.org/1.6.9/$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20salary

full code is here

what's the wrong in my code?

Upvotes: 1

Views: 109

Answers (2)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28475

Correct injector is $rootScope (case sensitive)

var sample=angular.module("sample",[]).run(['$rootScope', function($rootScope){

Also the order should be same in strings and function arguments

sample.controller('salary', ['$scope','$rootScope', function($scope, $rootScope){

Upvotes: 1

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41445

change the order. You need to have the same order in both arguments and string values

sample.controller('salary', ['$scope','$rootScope', function($scope, $rootScope){

Upvotes: 1

Related Questions