1

I'm developing several AngularJS applications and I'm looking for best practices how I can share code, especially directives, between these projects. The biggest problem I'm facing is the following:

Most of my directives contain at least one js file and a html template. The js file often contains a reference to the template. When I include the same js file from two different projects, what's the best way to handle the different paths?

There may be better ways to handle this situation I can't even think of. So, I would like to know if someone has experiences with this situation and how this is handled. Thanks!

3
  • 1
    This question might be a useful question, but the way it is worded currently, it lacks detail. If you present specific code which illustrates a problem, a specific fix can be provided. As this reads now, however, it just sounds like you want to start a discussion of possible options, rather than a solution. Commented Sep 28, 2015 at 13:57
  • In general, the term "Best practices" sets off a discussion flag for many people, who won't even read the content of the question. Commented Sep 28, 2015 at 13:59
  • I currently use templateoverride for situations like this, so whoever is using the directive can bring in the template and place it where ever they need and you have a templateoverride using $templateCache to properly find it in that project. Commented Sep 28, 2015 at 14:28

2 Answers 2

1

I find it helpful to use compiled HTML templates using a build tool like https://www.npmjs.com/package/grunt-angular-templates . There are plenty of Gulp/Grunt alternatives if that one doesn't suite your needs.

Then you will need to keep your templates in namespaced directories so that your consumer applications don't collide.

Then you when you just need to consume a single compiled JS file. The templates are compiled into it. If you need to override the templates in your applicatons, just use the template namespace convention to provide the overrides.

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

Comments

0

Maybe you can write your code in nodejs style (with CommonJS), locate your files in different folders and such, and then use browserify to combine your js code into one piece. HTML templates can be also easily transpilled into js files using angular template cache and some tool like this one for gulp or maybe some similiar for grunt.

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.