javascript - 如何处理 Angular 1.x 组件 URL 根?

标签 javascript angularjs angular-components

为了能够设置组件模板的 URL 根,在 iif 级别需要一个可用的配置对象。

(function () {
    var app = angular.module('myApp');
    app.component('myDashboard', {
        // templateUrl: HOW_TO_GET_ROOT_HERE + '/path/to/template.html'
        templateUrl: '/path/to/template.html'
    });
})();

到目前为止,我发现这可能的唯一方法是让模块对象添加一些自定义属性 - 但这样做确实让人感觉很讨厌。

// define
var app = angular.module('myApp', []);
app._config = {root: 'http://my.cdn.js'}
//...
// use
var app = angular.module('myApp');
app.component('myDashboard', {
   templateUrl: app._config.root + '/path/to/template.html'
}

还有其他更简洁的方法吗?

最佳答案

在 Angular 应用程序模块中声明一个新常量。然后利用 templateUrl 函数内的通用配置来形成 templateUrl。现在 templateUrl 函数可以有注入(inject)。

app.constant('config', { rootPath: 'http://my.cdn.js'});

组件

app.component('myDashboard', {
   templateUrl: function(config){
     return config.rootPath + '/path/to/template.html'
   }
}

关于javascript - 如何处理 Angular 1.x 组件 URL 根?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842628/

相关文章:

javascript - 将数据传递到 Angular 中的其他页面/组件

javascript - 从 JavaScript 中的对象获取值

javascript - 为什么没有定义参数?

javascript - 按顺序返回 promise

google-app-engine - 寻找一种使用ajax使用angularjs将文件上传到应用程序引擎的方法

angular - 为什么 Angular 坚持为组件使用自定义前缀?

javascript - 选择下拉值会显示一个文本框,但不存储传入其中的值

javascript - Papaparse 对奇怪字符的正确编码

javascript - 验证时使另一个字段无效

Angular - 在服务值发生变化时更新组件值