Javascript 角度组件相对模板URL

Javascript 角度组件相对模板URL,javascript,angularjs,components,angularjs-components,Javascript,Angularjs,Components,Angularjs Components,我正在开发一个模块化的angular应用程序,我在angular-module-1模块中定义了I文件夹路径(常量:BASE_path:“path/to/folder”)。我想在我的应用程序的另一个模块(angular-module-2)中的角度组件中重复使用该常数。我想在我的项目中多次使用这个常数 module.component("relationshipSearch", { templateUrl: BASE_PATH +'/link/to/other/folder', witch是将该常

我正在开发一个模块化的angular应用程序,我在angular-module-1模块中定义了I文件夹路径(常量:BASE_path:“path/to/folder”)。我想在我的应用程序的另一个模块(angular-module-2)中的角度组件中重复使用该常数。我想在我的项目中多次使用这个常数

module.component("relationshipSearch", {
templateUrl: BASE_PATH +'/link/to/other/folder',
witch是将该常量定义为在所有解决方案项目中可见的全局变量的最佳方法 以下是我的项目结构:

project
|-- angular-module-1
|   |-- angular-module-1.js
|   |-- angular-module-1.html
|-- angular-module-2
|   |-- angular-module-2.js
|   |-- angular-module-2.html

也许另一种方法会有所帮助。与其设置路径,不如只查看同一目录中的文件

您可以使用
require()
,例如:

template: require('./template-in-same-dir.html')

注意
模板
不是
模板URL

我要说的是,创建一个名为
angular common
的公共模块,您可以在其中放置
common
东西,比如common
service
factory
directive
constants
,等等

然后在
角公共
(这将是完全独立且可插入的)模块内添加常数。如下

//assuming `angular-common` is already defined
angular.module('angular-common').constant('commmonSetting', {
  'BASE_PATH': '/link/to/other/folder'
})
angular.module('app', ['angular-common', 'ngRoute', ...other dependencies..])
接下来,在
app
(这是主模块,将在
ng app
/
bootstrap
中使用)中插入此通用模块,如下所示

//assuming `angular-common` is already defined
angular.module('angular-common').constant('commmonSetting', {
  'BASE_PATH': '/link/to/other/folder'
})
angular.module('app', ['angular-common', 'ngRoute', ...other dependencies..])
当您想使用
BASE\u PATH
时,只需在
controller
/
组件中插入
commmonSetting
依赖项即可

app.component('myComponent', {
  templateUrl: function(commmonSetting){
    return commmonSetting.BASE_PATH +'/link/to/other/folder';
  },
  ....
})

很抱歉,在一个旧的帖子上发布了一个被接受的答案,但我想说的是,被接受的答案是不安全的。写这篇文章的安全方法是:

app.component('myComponent', {
  templateUrl: function($injector){
    var commmonSetting = $injector.get('namespace.CommmonSetting');
    return commmonSetting.BASE_PATH +'/link/to/other/folder';
  },
  ....
});

我问的是在不同的角度应用模块之间共享/重复使用常数的最佳方式。但标题问的是一个模板URL。我仍然认为我对您的具体问题的解决方案值得考虑。通过创建多个模块,您已经将它们耦合在一起,形成了一个字符串。似乎有点过分了。当然,烤蛋糕的方法有很多种,这取决于我们最喜欢哪一种口味,对吧!:)我在同一个文件夹中有许多模板,因此在TEMPLATEURL中使用require时需要定义一个常量路径。我不能使用我的ConstantHanks,因为我需要:)