簡體   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