Angular rotue provider not working with Require JS

85 views Asked by At

I'm fairly new to Angular JS. I was doing some practice demos with require JS along with angular routing using route provider in a well defined folder structure.

I tried searching for similar angular router problems but most do not have any folder structure, Their code was present in script tags.

I hope somebody has the patience to have a quick look and checkout why the route provider seems so non functional despite everything seeming to be done correct. :)

Anyway, I have an index.html that simply loads the require JS with a source

Index.html

<!DOCTYPE html>
<html>
    <head>  
    <title>MyApp</title>
    <meta charset="utf-8"/>

    <script data-main="assets/require/requireConfig" src="assets/vendor/require.js"></script>
    <link rel="stylesheet" href="assets/css/site.css">
    </head>

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

The Require JS source looks as follows where 'router' is the standard angular-router. 'myApp' would be the app I'm going to bootstrap.

The data source of Require JS

requirejs.config({
    baseUrl: 'assets',

    paths: {
        'myApp': 'require/myApp',
        'angular': 'vendor/angular',
        'router': 'vendor/angular-route',
        'domReady': 'vendor/dom-ready'
    },

    //Shim is also used to specify if one module must be loaded only after another
    //angular does not support AMD out of the box, put it in a shim
    shim: {
        'angular': {
            exports: 'angular'
        },
        'router': {
            deps: ['angular'],
            exports: 'router'
        }
    },

    // kick start application
    // The ./ represents the base URL given at the top
    deps: ['./require/startup']
});

So Now I'm going to run the startup.js to bootstrap my app file The following is how my startup.js looks

Angular Bootstrapping

 define([
    'require',
    'angular',
    'myApp'
], function (require, angular) {
    'use strict';

    require(['domReady!'], function (document) {
        angular.bootstrap(document, ['myApp']);
    });
});

And finally.... The Route Provider that is causing all the problems is defined in myApp.js

define(['angular','router'], function (angular) {
    'use strict';

    var myApp =  angular.module('myApp', ["ngRoute"]);

    myApp.config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/login', {
            templateUrl: '../../modules/login/login.html'
        });

        $routeProvider.when('/home', {
            templateUrl: '../../modules/home/homepage.html'
        });

    }]);
});

Ok, So I run the application on an nginx server.

http://localhost/login 
    OR
http://localhost/home

does not direct me to the required pages I have specified in the templates.

However

  1. If I specify a "$routeProvider.otherwise" it always redirects to it.

  2. If I change $routeProvider.when("/login") to $routeProvider.when("/"), The router opens up the login page but complains there is no controller associated with the view specified. (Hence my conclusion that the template URLs were pointing correctly)

  3. Deciding to create a controller 'loginController' throws the error that the 'controller is not registered'. For which when I digged around that is due to bootstrapping. (Wha?. So whats good about requireJS? But I'll leave that question for another time)

So, why such weird behavior by the route provider? I do not see it?

0

There are 0 answers