在声明多个 Controller 时,这显然是一个非常常见的问题,但我尝试关注此论坛或其他地方的每个修复似乎都不起作用(可能遗漏了一些东西)。
我有 app.js 文件和 3 个独立的 Controller 文件和一个服务文件。
我使用了其中一个 Controller ,一切正常。现在我试图重定向到另一个由 ProfileManagement Controller 处理的 View ,但它显示错误:
Error: [ng:areq] Argument 'ProfileManagementController' is not a function, got undefined
这是我在每个 Controller 文件和 app.js 开头的内容...
应用程序.js:
var app = angular.module('starter', ['ionic', 'starter.controllers', 'starter.services']);
app.run(function($ionicPlatform) {...
在 app.js 中,我在错误显示之前使用了这条路线:
.state('tab.home', {
url: '/home',
views: {
'tab-home': {
templateUrl: 'templates/tab-home.html',
controller: 'ProfileManagementController'
}
}
})
我的 Controller 在这里...
用户访问 Controller :
app.controller('UserAccessController', ['$scope', '$http', '$state', '$q', '$rootScope', 'CreateUserService', 'UserObjectService', function($scope, $http, $state, $q, $rootScope, CreateUserService, UserObjectService){
和 ProfileManagementController:
app.controller('ProfileManagementController', ['$scope', '$http', function($scope, $http){
}]);
还有 ionic 框架的选项卡项目模板,我默认得到了文件controller.js,我在其中评论了所有 Controller 但留下了第一行:
angular.module('starter.controllers', []);
//.controller('ProfileController', function($scope) {})
//
//.controller('OrdersController', function($scope) {
//
//})
//
//.controller('MoreOptionsController', function($scope, $stateParams) {
//
//})
//
//.controller('ConnectionsController', function($scope) {
//
//});
我在这里做错了什么导致出现错误消息? (实际与路由 tabs.home 关联的 View 实际加载但控制台显示错误。
谢谢,
最佳答案
您可能忘记了在 html 中添加脚本标签来定义您的 Controller 。
例如:
<script src="yourController.js"></script>
关于javascript - AngularJS - 错误 Controller 不是函数,未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33936451/