Angularjs 为什么将ng作用域添加到我的部分视图的javascript内联中并使警报无法工作?

Angularjs 为什么将ng作用域添加到我的部分视图的javascript内联中并使警报无法工作?,angularjs,angularjs-scope,partials,angularjs-view,Angularjs,Angularjs Scope,Partials,Angularjs View,我正在使用AngularJs和模板系统。 我想向每个模板添加特定的内联javascript脚本,添加与所选选项卡(主页|列表|设置)相关的警报框 Html呈现: 但是添加了ng范围,当您更改选项卡时,不会发出任何警报 <script type="text/javascript" class="ng-scope">alert("home")</script> 警报(“主页”) 我在这里举了一个例子: 还是在这里 警报(“template1”)出现在template1.

我正在使用AngularJs和模板系统。 我想向每个模板添加特定的内联javascript脚本,添加与所选选项卡(主页|列表|设置)相关的警报框

Html呈现: 但是添加了ng范围,当您更改选项卡时,不会发出任何警报

<script type="text/javascript" class="ng-scope">alert("home")</script>
警报(“主页”)
我在这里举了一个例子:

还是在这里

警报(“template1”)出现在template1.html中,但呈现为

<script type="text/javascript" class="ng-scope">alert("template1")</script>
警报(“模板1”)

通过对指令进行编码,可以找到一种解决方案:

局部使用:

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

警报(“延迟加载”);

我在

同样的过程

  • 创建angular-loadscript.js(从上面的链接)
  • 在应用程序中,将“ngLoadScript”用作资源依赖项

    var app=angular.module('YOUR_app_NAME',['ngResource','ngRoute',…,'ngLoadScript'])

  • 在部分情况下,请使用“text/javascript lazy”作为MIME类型

  • 一切都应按要求进行:

    /*global angular */
    (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();
              /*var f = new Function(code);
              f();*/
            }
          }
        };
      });
    
    }(angular));
    

    那个脚本应该存在吗?将类添加到脚本标记或任何其他标记都不会阻止脚本运行。。。。脚本标记中是否有文本?如果它被剥离以避免脚本过度编写,也不会感到惊讶。jQuery对他们的
    html()
    方法做了同样的事情,在DOM insertionI出现同样问题之前删除了所有scriot,因为我更新到angular 1.2,我的javascript代码不再使用partials模板加载。有没有办法做到这一点,但仍然使用“src”脚本标签上的属性?我在同一个文件中放置了多个javascript延迟,并且加载顺序不为jive=(改进?复制粘贴)您的意思是如何使其同步调用?
    /*global angular */
    (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();
              /*var f = new Function(code);
              f();*/
            }
          }
        };
      });
    
    }(angular));