javascript - AngularJS 包含一个位于文件夹中的文件

标签 javascript angularjs

当我做的时候

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

相关文章:

javascript - AngularJS ng-click 在编译尝试后未触发

javascript - onclick 打开子窗口并提交表单

javascript - 将 setInterval 存储在数组中

javascript - 你能在 three.js 中提供一个作为纹理加载的图像作为另一个纹理 mipmap 吗?

javascript - AngularJS 将字符串作为函数传递给 ng-click

javascript - 过滤搜索结果

javascript - 在 select2 中检索选定值的数组

javascript - 如何从 javascript 上的 getUserMedia() 获取超过 3 声道的音频输入

javascript - $location.path 在表单提交后不会改变路线

angularjs - 将新值添加到 bootstrap ui-select