从服务器获取脚本位置脚本、位置、服务器

2023-09-13 04:56:48 作者:动我女人者全撂倒

我有一个控制器(春季),返回应用程序版本(版本/获取),我需要使用这个版本,指定的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.