从服务器获取脚本位置 [英] Get script location from server

查看:126
本文介绍了从服务器获取脚本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器(春季),返回应用程序版本(版本/获取),我需要使用这个版本,指定的js文件的位置:

I have a controller (spring), that returns application version ("version/get") and I need use this version to specify location of js-file:

<script src="/r/{{appVersion}}/js/app.js"></script>

我怎样才能做到这一点使用JavaScript或angularjs?

How can I do this using javascript or angularjs?

我试图做这样的事情:

module.controller('ResourceLoaderController', [
    '$rootScope', '$scope', '$http',
    function ($rootScope, $scope, $http) {
        'use strict';

        $scope.getVersion = function() {
            $http.get('/version/get').then(function (response) {
                $rootScope.appVer = response.data;
            });
        };

        $scope.getVersion();
    }]);

和则:

<script src="js/controllers/ResourceLoaderController.js"></script>
<div ng-controller="ResourceLoaderController">
    <link rel="stylesheet" type="text/css" href="/r/{{appVer.text}}/css/app.css" charset="utf-8">
    <script src="/r/{{appVer.text}}/js/app.js"></script>
</div>

但我不能用&LT; D​​IV&GT; 在头...

推荐答案

试试这个你的 $ HTTP 成功里面。

Try this inside your $http success.

// use global document since Angular's $document is weak
var s = document.createElement('script'); 
s.src = '/r/'+response.data.text+'/js/app.js';
document.body.appendChild(s);

这将创建适当的src一个新的脚本元素。

It will create a new script element with proper src.

角过程中的页面已经被加载后,方可网页,
  脚本&GT; 标记已经被处理了一次由时间&LT建
  (脚本标记只运行过一次)。其他标记,如 IMG
  改变时它们的属性在屏幕的视觉外观
  该页面后,变化已经加载中...但如前所述,剧本
  仅在页面加载和前角处理一次,甚至然后
  都不能得到控制。

Angular processes the web page only after the page has been loaded and built by which time the <script> tag has been processed its one time (script tags are only ever run once). Other tags such as img will change the visual appearance of the screen when their properties change after the page has loaded ... but as mentioned, the script is only processed once and even then during page load and before Angular can ever get control.

所以,你需要追加你的脚本标签。或者您也可以通过服务器端的编码实现这一目标。

So you need to append your script tag. Or you may achieve this by server side coding.

这篇关于从服务器获取脚本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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