1

I have a new angular app and I can't figure out why my controller is not registering. The full error I get is:

The controller with the name 'DashboardController' is not registered.

I have the app module and dashboard controller in two separate files:

app.js:

var HtJobPortal = (function () {
    'use strict';

    angular.module('HtJobPortal', []);
})();

DashboardController:

(function () {
    'use strict';

    HtJobPortal.controller('DashboardController', []);

    var vm = this;
    vm.title = 'DashboardController';

})();

And my HTML is just attempting to extract the title:

<body ng-controller="DashboardController">
    {{title}}


    <!-- JS Scripts -->
    <script src="Scripts/jquery-3.1.1.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
    <script src="Scripts/angular.js"></script>

    <!-- Application Directive -->
    <script src="Scripts/controllers/app.js"></script>

    <!-- Controllers -->
    <script src="Scripts/controllers/DashboardController.js"></script>

</body>

Can anybody help point out what I'm missing here?

1 Answer 1

1

you don't need to add the IIFE function assign to a variable just remove it. The whole point of using IIFe is to reduce the time of the variable life. making a global variable just make the lifetime much longer

(function () {
    'use strict';

    angular.module('HtJobPortal', []);
})();

in the controller, file add the module reference to the controller and create a function to that controller

(function () {
    'use strict';
 angular.module('HtJobPortal')
    .controller('DashboardController',DashboardController);

    DashboardController.$inject = ['$scope'];

    function DashboardController($scope){      
       var vm = this;
       vm.title = 'DashboardController';
    }

})();

In the html add the ng-app directive also. since you are using the controlleras , use the correct syntaxes.

<body ng-app="HtJobPortal" ng-controller="DashboardController as vm">
    {{vm.title}}


    <!-- JS Scripts -->
    <script src="Scripts/jquery-3.1.1.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
    <script src="Scripts/angular.js"></script>

    <!-- Application Directive -->
    <script src="Scripts/controllers/app.js"></script>

    <!-- Controllers -->
    <script src="Scripts/controllers/DashboardController.js"></script>

</body>
Sign up to request clarification or add additional context in comments.

2 Comments

Stops the errors but doesn't output the title - just a blank screen
@Web Develop Wolf no problem bro :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.