gpt4 book ai didi

AngularJS - 添加 Controller 文件触发 Uncaught Error

转载 作者:行者123 更新时间:2023-12-02 01:31:33 26 4
gpt4 key购买 nike

我正在创建一个 angularJS 应用程序,并决定将 Controller 分成多个文件。这是我的:

为了声明模块,我有一个包含以下内容的文件:

文件:controllers.js

angular.module('starter.controllers', []);

我正在使用的其他 Controller 之一称为 ConnectionsController:文件:ConnectionsController.js

angular.module('starter').controller('ConnectionsController', ['$scope', '$http', function($scope, $http){

}]);

在 app.js 中,我有以下内容:文件:app.js

var app = angular.module('starter', ['ionic', 'starter.controllers', 'ConnectionsController','starter.MoreOptionsController','starter.OrdersController', 'starter.ProfileManagementController', 'starter.UserAccessController', 'starter.services']);

当我运行应用程序时,它触发了一个错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module ConnectionsController due to:
Error: [$injector:nomod] Module 'ConnectionsController' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

我在声明 Controller 或将它们链接到 app.js 文件时做错了什么?

谢谢,

更新:Index.html 内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/ConnectionsController.js"></script>
<script src="js/OrdersController.js"></script>
<script src="js/services.js"></script>

</head>
<body ng-app="starter">
<!--
The nav bar that will be updated as we navigate between views.
-->
<ion-nav-bar class="bar-stable">
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
<!--
The views will be rendered in the <ion-nav-view> directive below
Templates are in the /templates folder (but you could also
have templates inline in this html file if you'd like).
-->
<ion-nav-view></ion-nav-view>
</body>
</html>

最佳答案

该错误不言自明:您没有模块 ConnectionsController。请注意,当您声明主应用程序模块时,如

angular.module('starter', [
'ionic',
'starter.controllers',
'ConnectionsController',
// etc ...
]);

这意味着模块 starter 依赖于其他几个模块,在您的例子中是 ionicstarter.controllersConnectionsController。但是,ConnectionsController 是 Controller 名称,它不是模块。因此您不必将其列为模块依赖项。

关于AngularJS - 添加 Controller 文件触发 Uncaught Error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33879042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com