Search code examples
javascriptangularjsangular-ui-routeriif-function

Angularjs routing not working with controller and model using IIF funtion


I created a simple website with a configuration for routes. I am using IIF javascript for my modules and controllers but routing is not working.

It only works if I type the default localhost like this http://localhost:18689 but if try http://localhost:18689/maindashboard any route, or the $location.path('/addrequest') I get a 404.

Could be the IIF be the problem?

My modules is this :

(function () {
    "use strict";

    var clientAppModule = angular.module('clientAppModule', ["ngRoute", "ui.bootstrap", "ngResource"]);

    clientAppModule.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when("/maindashboard", {
            templateUrl: "app/maindashboard/maindashboard.html",
            controller: "mainDashboardController"
        })
        .when("/addrequest", {
            templateUrl: "app/addrequest/addrequest.html",
            controller: "addRequestController"
        })
        .otherwise({
            redirectTo: "/maindashboard"
        });

      $locationProvider.html5Mode(true);

     });
}());

The index page:

<!DOCTYPE html>
<html ng-app="clientAppModule">
<head>
    <title></title>
    <!-- CSS -->
    <link href="Content/css/font-awesome.css" rel="stylesheet" />
    <!-- Libraries -->
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Scripts/angular-resource.min.js"></script>
    <script src="Scripts/angular-ui/ui-bootstrap-tpls.min.js"></script>
    <!-- Code -->
    <script src="App/clientAppModule.js"></script>
    <script src="App/Index/indexController.js"></script>
    <script src="App/MainDashboard/mainDashboardController.js"></script>
    <script src="App/AddRequest/addRequestController.js"></script>
    <base href="/">
</head>
<body class="container">
    <h1>New Turning Point and Care Point Test Frame</h1>
    <div>

        <br /> 3 + 5 =
        {{3+5}}
        <br />

    </div>
    <div ng-view>
    </div>
</body>
</html>

The main dashboard controller is :

(function () {
    'use strict';

    var mainDashboardController = function ($scope) {
        $scope.TestObj = 'testmainDashboardController';
        $scope.showAddRequest = function () {
            $location.path('/addrequest');
        };
    };

    var app = angular.module('clientAppModule');
    app.controller("mainDashboardController", mainDashboardController);
})();

The addrequest controller is similar just a test


Solution

  • Your issue is that you're missing the $location parameter in mainDashboardController

    var mainDashboardController = function ($scope, $location)

    Also, if you plan on minimizing your code you should declare your dependencies when you create your controller using either inline syntax

    app.controller('mainDashboardController', ['$scope', '$location', mainDashboardController]);
    

    or the angular injector

    mainDashboardController.$inject = ['$scope', '$location']
    

    That way when the minimizer changes your parameter names to a or b or whatever, angular will know what they're supposed to be.