Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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'])
    .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
        $routeProvider.when('/add-event', {
                templateUrl: 'views/add-event.html',
                controller: 'formCtrl',
                controllerAs: 'eventCtl'
            })
            .otherwise({
                redirectTo: '/'
            });
        $locationProvider.html5Mode(true);
    }])
    .run(['$rootScope', function() {
        $rootScope.event = [];
    }]);

this js/controller.js

  angular.module('addEvent')
      .controller('formCtrl', ['eventFactory', function(eventFactory) {
          //$scope.event=[];
          this.event = eventFactory.getAllEvents();
          this.submitForm = function(form) {
              eventFactory.createEvent(angular.copy(form), this.event);
              // $scope.event.push(angular.copy(form));
              console.log($scope.event);
          }
      }])

services/eventFactory.js

angular.module('addEvent')
    .factory('eventFactory', function() {
        var eventFactory = {};
        var events = [];
        eventFactory.getAllEvents = function() {
            return events;
        }
        eventFactory.createEvent = function(event, eventList) {
            events.push(event);
            eventList = events;
            return eventList;
        }
        return eventFactory;
    })    

And at index.html I added script this way

<script src="./js/jquery-1.12.4.js"></script>
<script src="./js/bootstrap.js"></script>
<script src="./js/angular.min.js"></script>
<script src="./js/angular-route.js"></script>
<script src="./js/app.js"></script>
<script src="./js/controller.js"></script>
<script src="./services/eventFactory.js"></script>
share|improve this question

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

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

instead of

.run(['$rootScope',function(){
    $rootScope.event=[];
}]);
share|improve this answer
1  
thank you very much – NIL SAGOR Oct 10 '16 at 6:53

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>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.