0

I have an Spring + AngularJS Web Application with the following angular file structure. mainmodule.js - where all config and routing's are controller1.js controller2.js

my main page is home.jsp where I imported all these files. As project goes bigger, I guess we have little over 50 files overall. So instead of dumping them in home.jsp, I would like to load them in it's corresponding view files. But when I tried to load controller1.js in corresponding JSP file, I get the below error.

[ng:areq] http://errors.angularjs.org/1.4.4/ng/areq?p0=dashboardController&p1=not%20aNaNunction%2C%20got%20undefined

Can someone help me identify what is the issue and how can I achieve this?

BTW I am using ui-router instead of default ngRoute.

1 Answer 1

3

One way of dealing with that scenario is to define a resolve property on each route and assign it a function that returns a promise. The function can handle dynamically loading the script containing the target controller and resolve the promise once the load is complete. For example:

$routeProvider
.when('/customers',
    {
        templateUrl: '/app/views/customers.html',
        resolve: resolveController('/app/controllers/customersController.js')
    });

But the best solution to your problem would be to use RequireJS framework with AngularJS for dynamically loading controllers per view. There is very interesting source that explains about this : http://weblogs.asp.net/dwahlin/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs

RequireJS official website: http://requirejs.org

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

1 Comment

I would like to use the code which you post here, but I can't find resolveController method in ui-router. So can you post for any relevant site link?

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.