0

Sorry this will obviously be a beginner question. I have Angular up and running fine within a Grails application. Now I have defined a directive that is effectively a control needed. This directive uses 'templateUrl' - that also works fine.

However, this is the simple part that I just don't know. Where would I put includes to javascript libraries that are only needed by the directive? That is, statements like:

<link href="css/bootstrap.css" rel="stylesheet" />
<script src="js/bootstrap.js"></script>

I have put them in the main page for now, but that doesn't seem quite 'right'. These are dependencies of the directive, not of the page. From a maintenance perspective, if that directive were ever removed due to changes, how would anyone know to remove the other links? Note: my directive is already in an open statement - if that matters.

2 Answers 2

1

This is a larger question of dependency management. There really are 3 routes for doing this:

  1. Include it in the main index.html (like you did) and declare it as a dependency in your docs. Feel ugly? Sure. But it is how a lot of stuff is done.
  2. Use requirejs http://requirejs.org It is a module loader, and so at least the code itself can explicitly declare its dependencies.
  3. Use browserify http://browserify.org It is also a module loader, but following UMD/CommonJS, exactly like in node.

I started with #1, then #2 for a while, but recently shifted to #3. The files are cleaner, and using npm makes managing the dependencies far easier.

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

Comments

0

Loading all external resources inside index.html is the common approach (SPA load all its resources once), but its not a must.

Inorder to achieve your goal you need to use external tool, we use RequireJS (Browserify is good as well) this way you can control the sources you load into the page.

Checkout this:

RequireJS official site

Using RequireJS in Angular Applications

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.