我怎么能沿加载javascript文件NG-包括模板 [英] how can i load javascript file along with ng-include template

查看:147
本文介绍了我怎么能沿加载javascript文件NG-包括模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括一个模板到我的主要文件中使用以下code。

i am including a template into my main file using below code.

<p ng-include=" 'activity.html' "></p>

我的 activity.html 是这样的,

<script type="text/javascript" src="myjsfile.js"></script>

<p>Activity page contents</p>

但是JavaScript是没有得到与模板一起加载

我发现因此与同样一些问题,但没能找到一个有效的解决方案。
其中一个回答说,在上述主文件angularjs负荷的jQuery,使脚本标签将得到接受。但没有发现它的工作。

I found some questions related to the same in SO, but not able to find a working solution. One of the answer says load jquery above angularjs in main file, so that the script tag will get accepted. But not found it working.

推荐答案

我发现这个答案在这里<一个href=\"http://stackoverflow.com/questions/12197880/angularjs-how-to-make-angular-load-script-inside-ng-include\">load里面的脚本NG-包括的,遗憾的是它不接受的答案在那里。所以,我在这里给它提供了一些需要更多的更新。

I found this answer here load script inside ng-include, Unfortunately it was not the accepted answer there. So i am providing it here with some more updates for the need.

图书馆链接 rel=\"nofollow\">

Library link here

创建的js文件包含下面的脚本,并在主文件加载它。

(function (ng) {
    'use strict';

    var app = ng.module('ngLoadScript', []);

    app.directive('script', function() {
        return {
            restrict: 'E',
            scope: false,
            link: function(scope, elem, attr) {
                if (attr.type=='text/javascript-lazy') {
                    var code = elem.text();
                    var f = new Function(code);
                    f();
                }
            }
        };
    });
}(angular));

并加载 ngLoadScript 模块应用依赖性:

angular.module('myApp', [
    'ngLoadScript'
])

在部分(** activity.html )使用文/ JavaScript的懒惰键入属性值为剧本标签,而不是文/ JavaScript的:**

in Partial (**activity.html) use text/javascript-lazy as type attribute value for script tag instead of text/javascript:**

<script type="text/javascript-lazy">
    alert("Yup!");
</script>

多一点的加载外部JS做的:

我用下面code从 ddrive 加载脚本(在< STRONG> main.html中)

i have used below code from ddrive for loading scripts (in main.html)

function loadjscssfile(filename, filetype) {
    if (filetype == "js") {
        // if filename is a external JavaScript file
        var fileref = document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", filename);
    }
    else if (filetype == "css") {
        //if filename is an external CSS file
        var fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined") {
        document.getElementsByTagName("head")[0].appendChild(fileref)
    }
}

并在部分(activity.html):

<script type="text/javascript-lazy">
    loadjscssfile("myjsfile.js", "js");
</script>

这篇关于我怎么能沿加载javascript文件NG-包括模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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