3
votes

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.

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:

Error: [ng:areq] Argument 'ProfileManagementController' is not a function, got undefined

This is what I have in the beginning of each controller file and also 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:

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

My controllers are here...

UserAccessController:

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

and 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:

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.

Thanks,

1
Did you load the controller script to the html. I mean <script ...? - Dvir
you mean the index.html or the sub-views? - ksa_coder
Somewhere. index.html is better - Dvir
yes I am updating the post now to show the index.html - ksa_coder
oops forgot to add <script src="js/ProfileManagementController.js"></script> This actually fixed it. To close the question, if you want to post your recommendation and then I can select it as answer. Thanks - ksa_coder

1 Answers

1
votes

You probably forgot to add the script tag to define your controller within the html.

for example:

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