用角动态SRC下载脚本脚本、动态、SRC

由网友(想念褪不去的回忆)分享简介:角提供了通过动态加载动态名模板NG-包括。内联JS和CSS在这部分将加载罚款,但没有与动态URL下载脚本的好办法。我们需要下载脚本,相对的.html部分调用它们的路径。 (即我们有一个目录中的文件包括,并希望.html文件申报等脚本,它需要自身)。Angular provides was to dynamically...

角提供了通过动态加载动态名模板NG-包括。内联JS和CSS在这部分将加载罚款,但没有与动态URL下载脚本的好办法。我们需要下载脚本,相对的.html部分调用它们的路径。 (即我们有一个目录中的文件包括,并希望.html文件申报等脚本,它需要自身)。

Angular provides was to dynamically load templates with dynamic names via ng-include. Inline JS and CSS in that partial will load fine, but there is not a good way to download scripts with dynamic urls. We needed to download scripts, relative to the path of the .html partial calling them. (i.e. we had a directory with a file to include, and wanted the .html file to declare the scripts etc. it needed itself).

相对于this问题,这是加载脚本使用静态SRC在动态包含部分,我要动态包括创建一个脚本SRC在动态包含部分,即使用插值或通过运行它和角度的功能。

As opposed to this question, which is loading a script with a static src in a dynamically included partial, I want to include dynamically create the src for a script in a dynamically included partial, using either interpolation or running it through and angular function.

例如,我可能是动态加载的部分进入我的应用程序,从目录中还有其他一些资源的部分依赖。相反,单独下载的所有文件,我想留在局部的逻辑。我意识到,浏览器无法做到这一点,所以我将使用NG-src和允许角在这里做繁重。而不是分析每个脚本SRC标签相对于局部的,所以我会用一个函数来生成的网址,像这样的:

For example, I may be dynamically loading a partial into my app, from a directory in which there are several other resources that partial relies on. Instead of downloading all the files individually, I want to leave that logic in the partial. I realize that the browser cannot do this, so I'll use ng-src and allow Angular to do the heavy lifting here. Instead of parsing every script src tag relative to the partial, so I'm going to use a function to generate the urls, like so:

<script type="application/javascript" ng-src="prefixUrl('myUrl.js')"></script>

我该怎么办呢?

推荐答案

这里是一个要点

请注意,这将覆盖本地脚本指令中的角(在你的脚本标签检查模板)。你可以重命名指令,但我们并不需要这些功能(我们这些服务/指令被注入到页面上的新应用程序自举反正)。

Note that this will override the native script directive in angular (which checks for templates in your script tags). You could rename the directive, but we didn't need that functionality (we were injecting these services / directives into a newly bootstrapped app on the page anyway).

此假设你有动态下载脚本的一些方法。我们使用$脚本,但jQuery的或任何会的工作也一样,只是相应地更新服务。

This assumes you have some way of downloading scripts dynamically. We are using $script, but jquery or whatever would work too, just update the service accordingly.

而不是使用下面是什么,它​​使用的函数prefixUrl的,你可以很容易地重写剧本指令preFIX URL本身(通过attrs.ngSrc)不依赖于功能。

Instead of using what is below, which uses a function prefixUrl, you could easily rewrite the script directive to prefix the url itself (via attrs.ngSrc) without relying on the function.

我已经添加某一事件WidgetContentLoaded'(容易改变),将触发一旦所有的脚本下载。

I've added an event 'WidgetContentLoaded' (easy to change) that will fire once all the scripts are downloaded.

的JavaScript

Javascript

angular.module('myApp')
.run(function ($rootScope) {
    $rootScope.mixin = function(urls) {

        if (angular.isUndefined(urls) || urls == '') {
            return $q.when('no mixin url');
        }

        var deferred = $q.defer();

        //timeout our requests at 5 seconds
        var timeoutPromise = $timeout(function() { deferred.reject(null) }, 5000);

        //assume that $script or some other way of downloading scripts is present
        $script(urls, function() {
            $timeout.cancel(timeoutPromise);
            $rootScope.$safeApply(deferred.resolve(urls));
        });

        return deferred.promise;
    };

    $document.on('WidgetContentLoaded', function () {
        //put more interesting logic here... this is like $(document).ready() but for your included partial
        console.log('yay we loaded your scripts');
    });

})
.service('lazyScripts', ['$q', '$timeout', '$document', function ($q, $timeout, $document) {

    var promises = [];

    this.register = function (url) {
        promises.push($clotho.extensions.mixin(url));
    };

    $timeout(function() {
        $q.all(promises).then(function() {
            //broadcast event
            $document.triggerHandler('WidgetContentLoaded');
        })
    });
}])
.directive('script', function($parse, $rootScope) {
    return {
        restrict: 'E',
        terminal: true,
        compile: function(element, attr) {
            if (attr.ngSrc) {
                var scriptUrl = $parse(attr.ngSrc)($rootScope);
                 lazyScripts.register(scriptUrl);
            }
        }
    };
});

和你的HTML

<script type="application/javascript" ng-src="prefixUrl('inlineCalledScript.js')"></script>

<style type="text/css">
    .greenListItem {
        color: #44bb44;
    }
</style>

<ul>
    <li>This is a dynamically loaded template.</li>
    <li>Note that angular must already be bootstrapped, with the new script directive above. This will not work in your index.html file</li>
    <li class="greenListItem">Inline CSS works!</li>
</ul>

<!-- this would work without problems -->
<div ng-include="prefixUrl('anotherPartial.html')"></div>
阅读全文

相关推荐

最新文章