当我做的时候
/sites/all/themes/theme/js/controller.js
$scope.template = 'partials/tpl.html'
/sites/all/themes/theme/js/index.html
<div ng-include='template'></div>
/sites/all/themes/theme/js/partials/tpl.html
<b>Ho boy!</b>
我基本上把所有与 angular 相关的东西都移到了 js 文件夹中。
它返回 localhost/partials/tpl.html
而我需要 localhost/sites/all/themes/js/partials/tpl.html
。如何在不使用绝对路径的情况下解决该问题?
最佳答案
我使用 $httpProvider.interceptors 解决了这个问题。以下为所有请求添加“myAppDirectory”。
$httpProvider.interceptors.push(function($q) {
return {
'request': function(config) {
config.url = 'myAppDirectory/'+config.url;
return config;
},
};
});
常规的 ng-include,例如:
<div ng-include="'user/info.html'">
将加载myAppDirectory/user/info.html
您可能想做一些异常(exception)。就我而言:
$httpProvider.interceptors.push(function() {
return {
'request': function(config) {
// ignore api calls and ui-bootstrap templates.
if (config.url.indexOf('api')==-1 && config.url.indexOf('template')==-1) {
config.url = 'myAppDirectory/'+config.url;
}
return config;
},
};
});
关于javascript - AngularJS 包含一个位于文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394627/