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'];
  
  functionrun($rootScope) {
    $rootScope.events = [];
    console.log($rootScope.events);
  }
  
  functionDefaultController() {
    var vm = this;
  }
<scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="demo"><divng-controller="DefaultController as ctrl"></div></div>

Post a Comment for "Angularjs: Uncaught Referenceerror $rootscope Is Not Defined"