2016-10-10 9 views
0

私が実行しようとしていますを定義し、このメッセージ持っていないされています。ここ

Uncaught ReferenceError: $rootScope is not defined at app.js line 12

私のjsでは/これは/ controller.jsを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 = []; 
    }]); 

app.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); 
      } 
     }]) 

サービス/ 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; 
    })  

そしてindex.htmlので、私はあなたが含まれるように忘れてしまったrun()方法

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

代わりの

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

答えて

4

スクリプトを追加しましたrun$rootScopeサービスあなたがエラー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>

+1

はどうもありがとうございまし参照理由ですパラメータとして機能 –

1

$rootScopeを注入する必要があり、このよう

<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> 
関連する問題