繁体   English   中英

AngularJS包含位于文件夹中的文件

[英]AngularJS include a file located in a folder

当我做

/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

你可能想做一些例外。 就我而言:

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

index.html内的<head>元素顶部添加<base href="/sites/all/themes/theme/js/">

<html>
  <head>
    <base href="/sites/all/themes/theme/js/">
    ...

参考

相关链接

请务必检查所有相关链接,图像,脚本等。您必须在主html文件(<base href =“/ my-base”>)的头部指定url base,或者必须使用绝对URL(从/)到处都是因为相对网址将使用文档的初始绝对网址解析为绝对网址,这通常与应用程序的根目录不同。

强烈建议使用从文档根目录启用历史记录API运行Angular应用程序,因为它会处理所有相关链接问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM