2

The RequireJS is not resolving dependency properly when the routes is of multiple levels as in http://www.example.com/profile/view. If I just have http://www.example.com/view, the controller dependency is resolved properly.

My bootstrap.js

require.config({
    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:

-rootdir
    - public
          - res
              - js
              - css

When I use this route http://www.example.com/profile/view, all the dependency modules are resolved with base url as http://www.example.com/profile/res/js/controller.js, which does not exist in this path http://www.example.com/profile.

If I change the route to http://www.example.com/view (just one level), dependencies are resolved with this base url http://www.example.com/res/js/controller.js

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

1
  • Minor note: priority is a RequireJS 1.x option which was removed in the 2.x series whereas shim is an option that was introduced in the 2.x series. Commented Jan 28, 2015 at 17:16

1 Answer 1

2

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:

$stateProvider
  .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'] 
} 

require.config(cfg);  

The most important think here, because of the:

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

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

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for spending time on this. Checked out your code and found that adding base tag with just '/' fixed this. I tried this with full site url, which did not work. Thanks again for your help.
Great to see that, enjoy mighty ui-router :)

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.