简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM