Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS App: How to include .js files into index.html

I'm new to angularJS. I managed to build a phonegap app using angularJS. The app is ok and running just fine. The problem is, now that I have a little more understanding on how angularJS works (at least I think I have), I'm worried about my app files structure and code maintainability. My project follows the angular-seed pattern. Angular Seed on GitHub where I have this structure:

js    app.js    controllers.js    directives.js    services.js    filters.js 

After researching how to structure apps with angularJS, I've found some very nice articles about it: Building Huuuuuge Apps with AngularJS and HOW TO STRUCTURE LARGE ANGULARJS APPLICATIONS

My question seems very silly to me but I couldn't find a way out. I managed to separate my controllers into different files and I have now this structure:

scripts     controllers              LoginCtrl.js              HomeCtrl.js              AboutCtrl.js              ClientCtrl.js  

The thing that I'm struggling with is that in the way that my app was before, I had only one controller.js file. In my index.html file, I load all the script files normally using the script tag. Now that I have 4 different .js files for my controllers, how do I load them into my index.html file? Do I have to load them all there?

It doesn't look right to me load all the scripts in the index.html file like that (I know that having one file the code will be loaded in the same way, it is just weird to me having many script tags packed there). I've found the ng-boilerplate project on github and on their index.html they load the scripts kind of dynamically. The problem is, I can't start a new ng-bolierplate right now, and I couldn't find how they did it. Sorry for the long text. Thanks!

like image 225
jpgrassi Avatar asked Dec 17 '13 12:12

jpgrassi


People also ask

HOW include JavaScript in index HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

What is index HTML in Angularjs?

The controller. js Javascript file has an Angular controller that is registered with the app. js Angular module and contains business logic (programming between end UI and database). index. html is the view page where I place my html code and loaded app.

Where do I load js files?

You can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.


2 Answers

Referencing each script file in its own tag in index.html is definitely one way to do it, and the most straightforward. However, if you want something cleaner, take a look at one of the various guides to combining Angular with RequireJS:

http://www.startersquad.com/blog/angularjs-requirejs/

Does it make sense to use Require.js with Angular.js?

This approach is aimed primarily at solving the problem of what order to load the files in order to satisfy dependencies, but it does also have the consequence of cleaning up the index.html...albeit at the cost of additional configuration elsewhere.

like image 53
S McCrohan Avatar answered Oct 06 '22 00:10

S McCrohan


Use http://browserify.org/

You get very simple and elegant commonjs modules and lots of amazing libraries from node.js in your browser.

The code gets bundled into single js file.

like image 29
Sava Jcript Avatar answered Oct 05 '22 23:10

Sava Jcript