Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have one function in a JS file which should ideally be used by multiple html pages. I don't want to duplicate this function to another file. Currently, my js file starts like this:

(function(){
     var app = angular.module('Project1', []);

and the first html that has been using this JS is obviously called Project1. I want 'Prject2' html to use this JS to, and I tried this:

(function(){
     var app = angular.module('Project1', 'Project2' []);

However, this doesn't work. Any idea of how I can utilize this AngularJS file for multiple html pages without duplicating the desired functions?

share|improve this question

simply add your js reference (and angular refrences) into both of your html pages do note that add them after adding angularjs references

share|improve this answer
    
i do the same for the must common functions i use. all in a js file and load it in the main html. – AlainIb 17 hours ago
    
I did not quite understand this answer. Can you please elaborate? – GuyTal 17 hours ago
    
If you mean to just add the .js reference to both html, this does not solve my problem since I still don't know which project I refer to in the angular.module braces. – GuyTal 17 hours ago

You can create your first module as you did in your first sample.

Your next sample can then use the 'Project1' by setting up a dependency to that module, like so:

var app = angular.module('Project2', ['Project1']);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.