Skip to content Skip to sidebar Skip to footer

Angularjs: Uncaught ReferenceError $rootScope Is Not Defined

I am trying to run and got this message: Uncaught ReferenceError: $rootScope is not defined at app.js line 12 here is my js/app.js angular.module('addEvent', ['ngRoute']) .c

Solution 1:

You need to inject $rootScope in the run() method

.run(['$rootScope',function($rootScope){
    $rootScope.event=[];
}]);

instead of

.run(['$rootScope',function(){
    $rootScope.event=[];
}]);

Solution 2:

You forgot to include the $rootScope service in the run function as a parameter that's why you see the error Uncaught ReferenceError: $rootScope is not defined

angular
  .module('demo', [])
  .run(run)
  .controller('DefaultController', DefaultController);
  
  run.$inject = ['$rootScope'];
  
  function run($rootScope) {
    $rootScope.events = [];
    console.log($rootScope.events);
  }
  
  function DefaultController() {
    var vm = this;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
  <div ng-controller="DefaultController as ctrl">
  </div>
</div>

Post a Comment for "Angularjs: Uncaught ReferenceError $rootScope Is Not Defined"