修改NG-include指令 [英] modifying ng-include directive

查看:528
本文介绍了修改NG-include指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在某个自定义位置例如举办角应用。 HTTP://本地主机/ collosys 然后我不得不改变所有的NG-包括有/ collosys /对于所有的地址,因为我使用绝对地址指的是任何HTML文件。

if I host an angular app at some custom location e.g. http://localhost/collosys then i have to change all the ng-include to have "/collosys/" for all the addresses, given that i am using absolute address for referring to any html file.

我这是怎么做的,目前

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

然后重写NG-包括标签

and then in rewrite ng-include tag

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

我可以修改的NG-包括标签本身,以便它能够preFIX所有地址的主机地址

Can i modify the ng-include tag itself, so that it can prefix all the addresses with the hosting address

修改

我读的地方,angularjs指令是可扩展的,关于如何扩展angularjs ngInclude指令任何例子?什么例子?

I read somewhere that angularjs directives are extensible, any example on how to extend angularjs ngInclude directives?? any examples??

推荐答案

我解决了使用$ httpProvider.interceptors这个问题。下面prependsmyAppDirectory所有请求。

I solved this issue using $httpProvider.interceptors. The following prepends 'myAppDirectory' to ALL requests.

$httpProvider.interceptors.push(function($q) {
    return {
     'request': function(config) {
         config.url = 'myAppDirectory/'+config.url;
         return config;
      },
    };
  });

一个经常NG-包括,如:

A regular ng-include, such as:

<div ng-include="'user/info.html'">

将加载的 myAppDirectory /user/info.html

Will load myAppDirectory/user/info.html

您可能想作出一些例外。在我的情况:

You probably want to make some exceptions. In my case:

  $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;
      },
    };
  });

这篇关于修改NG-include指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆