1

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

有没有其他更简洁的方法来做到这一点?

4

1 回答 1

1

在 Angular 你的应用模块中声明一个新的常量。然后利用函数内部的那个通用配置templateUrl来形成templateUrl。现在templateUrl函数可以有注射剂。

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

零件

app.component('myDashboard', {
   templateUrl: function(config){
     return config.rootPath + '/path/to/template.html'
   }
}
于 2016-11-28T11:16:44.777 回答