简体   繁体   English

AngularJS - 错误控制器不是函数,未定义

[英]AngularJS - Error controller is not a function, got undefined

This is a very common problem apparently when declaring multiple controllers but every fix on this forum that I tried to follow or somewhere else, didn't seem to work (probably am missing something). 这是一个非常常见的问题,显然在宣布多个控制器时,但是我试图在这个论坛上或其他地方修复了这个论坛,但似乎没有用(可能遗漏了一些东西)。

I have app.js file and 3 separate controller files and a services file. 我有app.js文件和3个独立的控制器文件和一个服务文件。

I used one of the controllers and everything worked fine. 我使用了其中一个控制器,一切正常。 Now I am trying to redirect to another view which handled by ProfileManagement controller, but it is showing the error: 现在我正在尝试重定向到由ProfileManagement控制器处理的另一个视图,但它显示错误:

Error: [ng:areq] Argument 'ProfileManagementController' is not a function, got undefined 错误:[ng:areq]参数'ProfileManagementController'不是函数,未定义

This is what I have in the beginning of each controller file and also app.js... 这是我在每个控制器文件的开头和app.js ...

app.js: app.js:

var app = angular.module('starter', ['ionic', 'starter.controllers', 'starter.services']);

app.run(function($ionicPlatform) {...

and in app.js, I am using this route before the error shows: 在app.js中,我在错误显示之前使用此路由:

.state('tab.home', {
    url: '/home',
    views: {
      'tab-home': {
        templateUrl: 'templates/tab-home.html',
        controller: 'ProfileManagementController'
      }
    }
  })

My controllers are here... 我的控制器在这里......

UserAccessController: UserAccessController:

app.controller('UserAccessController', ['$scope', '$http', '$state', '$q', '$rootScope', 'CreateUserService', 'UserObjectService', function($scope, $http, $state, $q, $rootScope, CreateUserService, UserObjectService){

and ProfileManagementController: 和ProfileManagementController:

app.controller('ProfileManagementController', ['$scope', '$http', function($scope, $http){

}]);

Also with tabs project template of ionic framework, I got the file controller.js by default where I commented all controllers but left the first line: 还有使用离子框架的标签项目模板,默认情况下我得到了文件controller.js,其中我评论了所有控制器,但是留下了第一行:

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

//.controller('ProfileController', function($scope) {})
//
//.controller('OrdersController', function($scope) {
//  
//})
//
//.controller('MoreOptionsController', function($scope, $stateParams) {
//  
//})
//
//.controller('ConnectionsController', function($scope) {
//  
//});

What am I doing wrong here that leads to the error message? 我在这里做错了什么导致了错误信息? (the view actually associated with the route tabs.home actually loads but the console shows the error. (视图实际上与路径tabs.home相关联,但是控制台显示错误。

Thanks, 谢谢,

You probably forgot to add the script tag to define your controller within the html. 您可能忘记添加脚本标记以在html中定义控制器。

for example: 例如:

<script src="yourController.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM