繁体   English   中英

如何使用Angular.js和Javascript动态设置脚本src路径

[英]How to set script src path dynamically using Angular.js and Javascript

我需要一个帮助,我需要通过Angular.js / Javascript / Jquery动态设置js / css路径名,下面解释我的代码。

<script src="/crm/controller/productController.js" type="text/javascript"></script>

假设我有上面的script标签。这里我的路径是/crm/controller/productController.js 。我需要在我的配置文件中设置/crm/ ,我将在每个script标签中设置这个路径名。在php中,它像下面。

define("USER_SITE_URL", "http://www.absclasses.com/");
<script type="text/javascript" src="<?php echo SITE_URL;?>js/jquery-1.9.1.min.js"></script>

由于我没有使用PHP。 我如何使用Angular.js或Javascript / Jquery进行制作。

您可以如下使用HTML 基本标记

<base href="http://www.absclasses.com/" />

如果要使用angular config块执行此操作,则还可以使用以下javascript代码进行一些修改。

document.write("<base href='http://" + document.location.host + "' />");

您还可以将store document.location.host定义为变量,然后使用它。

在javascript中,您可以设置全局变量。

function globalSource() {
    jqueryUrl = "http://xxxxxxx",
    angularUrl = "http://xxxxxx"
}

当您想使用时。

(function() {
  globalSource();
  //Get your dom and change attributes src link
  //ex: var xx = document.getElementsByTagName('script')[0].setAttribute("src", window.jqueryUrl );
})();
<xsl:variable name="Path" select="/crm/controller/"></xsl:variable> <!-- Global path variable. -->
<xsl:variable name="myScriptPath" select="concat($Path, 'productController.js')"></xsl:variable> <!-- Relative script path variable. -->
<script src="{$myScriptPath}"/> <!-- Attach script. -->

暂无
暂无

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

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