修改 ng-include 指令

2023-12-04

如果我在某个自定义位置托管一个有角度的应用程序e.g. http://localhost/collosys然后我必须将所有 ng-include 更改为所有地址都包含“/collosys/”,因为我使用绝对地址来引用任何 html 文件。

这就是我目前正在做的

<script>
    var baseUrl = document.location.pathname;
    document.write('<base href="' + document.location.pathname + '" />');
</script>

然后重写 ng-include 标签

FROM: <div data-ng-include="'/Generic/csgrid/grid-buttons.html'"></div>
TO: <div data-ng-include=" baseUrl + 'Generic/csgrid/grid-buttons.html'"></div>

我可以修改 ng-include 标签本身,以便它可以在所有地址前加上托管地址

EDIT

我在某处读到 AngularJS 指令是可扩展的,有关于如何扩展 AngularJS ngInclude 指令的示例吗?有例子吗??


我使用 $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'">

将加载我的应用程序目录/用户/info.html

您可能想要做出一些例外。就我而言:

  $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;
      },
    };
  });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

修改 ng-include 指令 的相关文章

随机推荐