suchen

Heim  >  Fragen und Antworten  >  Hauptteil

angular.js - angularjs的include指令,include的页面无法加载js

1

2

<code><p ng-include="'project_index.jsp'">

</code>

project_index.jsp中:

1

2

<code><script type="text/javascript" ng-src='{{generateURL("/scripts/tab.js")}}'></script>

</code>

生成的url是正确的,在浏览器中可以访问,但是我看过请求的记录,这个js没有被加载。所以js中的内容也无法执行。请问我的做法的问题在哪儿?感谢回答~

phpcn_u1582phpcn_u15822780 Tage vor580

Antworte allen(1)Ich werde antworten

  • 黄舟

    黄舟2017-05-15 16:52:36

    在stack上找到的。亲测有用。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    <code>(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 s = document.createElement("script");

          s.type = "text/javascript";               

          var src = elem.attr('src');

          if(src!==undefined)

          {

              s.src = src;

          }

          else

          {

              var code = elem.text();

              s.text = code;

          }

          document.head.appendChild(s);

          elem.remove();

        }

      }

    };

    });

    }(angular));

    </code>

    讲上述代码写成一个js文件,然后将此module ngLoadScript注入到你当前的应用中,然后在<script>标签中使用type="text/javascript-lazy"即可。
    This angular module have been created by @subudeepak(https://github.com/subudeepak) based on the code posted by @endorama (https://github.com/endorama)
    * (based upon the code
    * posted by @olostan (https://github.com/olostan) )

    Antwort
    0
  • StornierenAntwort