简体   繁体   English

如何使用AngularJS将一个.js文件用于多个.html页面

[英]How to use one .js file for multiple .html pages using AngularJS

I have one function in a JS file which should ideally be used by multiple html pages. 我在JS文件中有一个函数,理想情况下应由多个HTML页面使用。 I don't want to duplicate this function to another file. 我不想将此功能复制到另一个文件。 Currently, my js file starts like this: 目前,我的js文件开始如下:

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

and the first html that has been using this JS is obviously called Project1. 并且使用此JS的第一个html显然称为Project1。 I want 'Prject2' html to use this JS to, and I tried this: 我希望'Prject2'html使用此JS,并且尝试了此操作:

(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? 关于如何在不复制所需功能的情况下如何将AngularJS文件用于多个html页面的任何想法?

只需将您的js参考(和角度引用)添加到两个html页面中,请注意,在添加angularjs参考后添加它们

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: 然后,您的下一个示例可以通过对该模块设置依赖项来使用“ Project1”,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM