4

I have been working on an angular.js app that is growing wit many controllers, all of the files are included in the index.html file, even if the current view does not use one of them, this apply to providers and other modules too.

how do i call only the controller that is needed depending on the routes, in the index.html?

i have an index.html file with the many script tags:

<script src="controller1.js"></script>
<script src="controller2.js"></script>
<script src="controller3.js"></script>

Update: A better explanation of the question + answer. http://weblogs.asp.net/dwahlin/archive/2013/05/22/dynamically-loading-controllers-and-views-with-angularjs-and-requirejs.aspx

2
  • 1
    what do you mean by "call"? To load a specific js file? Commented Sep 21, 2013 at 16:00
  • yes, without the need to writte all the script tags. Commented Sep 21, 2013 at 17:27

2 Answers 2

1

What you are looking for is asynchronous modules and a loader that can pull them from the server. RequireJS is the first one that springs to mind. A seed project with examples of how to marry RequireJS and AngularJS can be found here.

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

1 Comment

It seams that RequireJS is what i am need, i will try it and post the results.
0

You should let angular handle it for you, based on the route or url. you best option is to look at the routeProvider, the documentation is here.

usage looks like this:

$routeProvider.when('/Book/:bookId/ch/:chapterId', {
    templateUrl: 'chapter.html',
    controller: ChapterCntl
});

Another option would be using Ui router, which is an extension of the routeprovider. here is the project page.

Comments

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.