Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何处理Angular 1.x组件URL根?_Javascript_Angularjs_Angular Components - Fatal编程技术网

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

Javascript 如何处理Angular 1.x组件URL根?,javascript,angularjs,angular-components,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' });

为了能够设置组件模板的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'
    });
})();
到目前为止,我发现唯一可行的方法是让module对象添加一些自定义属性——但这样做确实让人感觉很不舒服

// 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'
}

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

在应用程序模块中声明一个新常量。然后利用
templateUrl
函数中的公共配置来形成
templateUrl
。现在
templateUrl
函数可以有可注入项

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

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

是的,显然可以通过这种方式注入服务——使用带有返回组件属性值的param依赖项的函数。