requirejs with angular - not resolving controller dependency with nested route

The RequireJS is not resolving dependency properly when the routes is of multiple levels as in If I just have, the controller dependency is resolved properly.

My bootstrap.js

    baseUrl : 'res/js',
    paths: {
        routeResolve: 'routeResolve',
        'domReady': 'lib/domReady',
        angular: 'lib/angular',
        angularRoute: 'lib/angular-route',
        angularResource: 'lib/angular-resource',
        angularSanitize: 'lib/angular-sanitize',
        cssPath : '../css'
    map: {
      '*': {
        css: 'lib/require-css/css.min'
    shim: {
        'angular': {'exports': 'angular'},
        'angularRoute': {deps : ['angular']},
        'angularResource': {deps : ['angular']},
        'angularSanitize': {deps : ['angular']}
    priority: ['angular']

Folder structure:

    - public
          - res
              - js
              - css

When I use this route, all the dependency modules are resolved with base url as, which does not exist in this path

If I change the route to (just one level), dependencies are resolved with this base url

There should be a configuration issue which I am missing, but I could not find a solution for this.



I created working plunker here. It is based on the answer to angular-ui-router with requirejs, lazy loading of controller. I would exepct that the issue will be related to HTML setting <base href...

There is an example of the state, which in resolve loads lazily controller via requireJS:

  .state("first", {
    url: "/firstr",
    template: "<div>The message from ctrl: {{message}}</div>",
    controller: "FirstCtrl",
    resolve: {
      loadOtherCtrl: ["$q", function($q) {
        var deferred = $q.defer();
        require(["FirstCtrl"], function() { deferred.resolve(); });
        return deferred.promise;

There is the main.js for that example:

var cfg = {

    baseUrl: "res/js/",

    // alias libraries paths
    paths: { 

        // here we define path to NAMES
        // to make controllers and their lazy-file-names independent

        "TopMenuCtrl": "Controller_TopMenu", 
        "ContentCtrl": "Controller_Content", 
        "OtherCtrl"  : "Controller_Other",  
        "FirstCtrl"  : "Controller_First",
        "app"  : "../../app",  

    deps: ['app'] 


The most important think here, because of the:

$locationProvider.html5Mode({enabled: true});

is the setting of the base in the index.html:

  var urlBase = document.location.pathname;
  document.write('<base href="'+ urlBase +'" />')

Because I need here to be sure that this will work in plunker I do generate that dynamically, but in your case it could be just <base href="/" /> or some other setting, which will "teach" all the web where to search for resources

Check it here in action. Read more here


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.