Angular 组件相对模板Url

Saa*_*aad 2 javascript components angularjs 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',
Run Code Online (Sandbox Code Playgroud)

巫婆是将此常量定义为所有解决方案项目中可见的全局变量的最佳方法这是我的项目结构:

project
|-- angular-module-1
|   |-- angular-module-1.js
|   |-- angular-module-1.html
|-- angular-module-2
|   |-- angular-module-2.js
|   |-- angular-module-2.html
Run Code Online (Sandbox Code Playgroud)

Pan*_*kar 6

我想说的是创建一个名为作为公共模块angular-common,你可以把common东西一样常见servicefactorydirectiveconstants,等。

然后在内部添加常量angular-common(这将是完全独立且可插入的)模块。像下面

//assuming `angular-common` is already defined
angular.module('angular-common').constant('commmonSetting', {
  'BASE_PATH': '/link/to/other/folder'
})
Run Code Online (Sandbox Code Playgroud)

接下来,将这个公共模块注入app(这是主模块,将在ng-app/ 中使用bootstrap),如下所示

angular.module('app', ['angular-common', 'ngRoute', ...other dependencies..])
Run Code Online (Sandbox Code Playgroud)

当你想用BASE_PATH刚注入commmonSetting的依赖性controller/component不管你想要的。

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