为了能够设置组件模板的 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/