Angular.js:未定义的ReferenceError $ rootScope未定义

编程入门 行业动态 更新时间:2024-10-28 16:27:12
本文介绍了Angular.js:未定义的ReferenceError $ rootScope未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试运行并收到以下消息:

I am trying to run and got this message:

未捕获的ReferenceError:在app.js第12行中未定义$ rootScope

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

这是我的js/app.js

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 = []; }]);

此js/controller.js

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

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; })

在index.html上,我以这种方式添加了脚本

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>

推荐答案

您需要在run()方法中注入$rootScope

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

代替

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

更多推荐

Angular.js:未定义的ReferenceError $ rootScope未定义

本文发布于:2023-10-30 12:24:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1542832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   js   Angular   ReferenceError   rootScope

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!