The controller with the name 'MyPageController' is not registered Error (Angular1.5.3)

78 views Asked by At

I,m working on a quite old project which using Angular1.5.3 and now I want to add a new component. I have configed everything as the other component, now the newpage can be display(.html wroks fine) , but the data is not loaded(controller). and I got the error:

The controller with the name 'MyPageController' is not registered

here is some code of the project:

in mypage.controller.js:

(function () {
  'use strict';

  angular
    .module('forecastAngular')
    .controller('MypageController', MypageController);

  /** @ngInject */
  function MyPageController($scope, $rootScope, $state, $http, $timeout, usSpinnerService, dataFactory, $q, CommonServices, baseUrl) { 
... 
}

and in the index.route.js:

(function () {
  'use strict';

  angular
    .module('forecastAngular')
    .config(routerConfig);

  /** @ngInject */
  function routerConfig($stateProvider, $urlRouterProvider) {

  $stateProvider
  .state('mypage', {
        url: '/mypage',
        views: {
          'body': {
            templateUrl: 'app/myPage/mypage.html',
            controller: 'MypageController',
          }
        },
        data: {
          authorizedRoles: ['Admin', 'Manager', 'Director', 'Registered User', 'HR'],
          menuName: "mypage",
          isShowSearch: false,
          isShowDomain: false
        }
      });
}

and I also searched for this problem , someone said inject the controller use :

angular.module('myApp', []).controller('MyController', [function() {
  // ...
}]);

but I tried . not work for me . and the other component is config like what I have paste above. and it works fine.

can any one tell me how can I register the controller in right way?

2

There are 2 answers

1
Dariush Salami On

The function's name is wrong.

.controller('MyPageController', MyPageController);

Should be:

.controller('MyPageController', UtforcastController);

This function MyPageController does not exist.

0
Hassan Faghihi On

you said your module name is forecastAngular, but in another code, later on you created another module, which reference to no other, and it does create an empty controller

Note:

module(x) is getter

module(x, []) is setter

So you trying to get a module that you may not defined in here:

angular
    .module('forecastAngular')
    .config(routerConfig);