为什么NG-范围添加到我的局部视图中的javascript内联,使警报不工作? [英] Why ng-scope is added to javascript inline of my partial view and makes alert not working?

查看:282
本文介绍了为什么NG-范围添加到我的局部视图中的javascript内联,使警报不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是模板系统AngularJs。
我想补充的具体内嵌的JavaScript脚本,每个模板添加警告框关于所选标签(主页|列表|设置)

HTML渲染:
但NG-范围添加,当您更改选项卡的任何警报。

 <脚本类型=文/ JavaScript的阶级=NG-范围>警报(家)< / SCRIPT>

我可以做这里的例子:

http://bit.ly/HWcN1H

或此

plunkr例如警惕(template1的)present到template1.html而是呈现为

 <脚本类型=文/ JavaScript的阶级=NG-范围>警报(template1的)< / SCRIPT>


解决方案

我已经改善endorama的在的 github上

同样的过程。


  1. 创建(从上面的链接)的角loadscript.js

  2. 在您的应用程序使用ngLoadScript'作为一个资源依赖关系。

    变种应用程式= angular.module('YOUR_APP_NAME',['ngResource','ngRoute',...,'ngLoadScript']);


  3. 在您的局部使用文/ JavaScript的懒惰作为MIME类型。


一切都应该工作的要求:

  / *全球角度* /
(函数(NG){
  使用严格的;  VAR应用= ng.module('ngLoadScript',[]);  app.directive('脚本',函数(){
    返回{
      限制:'E',
      适用范围:假的,
      链接:功能(范围,ELEM,ATTR)
      {
        如果(attr.type ===文/ JavaScript的懒')
        {
          变种S =使用document.createElement(脚本);
          s.type =文/ JavaScript的;
          变种SRC = elem.attr(SRC);
          如果(SRC!==未定义)
          {
              s.src = SRC;
          }
          其他
          {
              变量code = elem.text();
              s.text = code;
          }
          document.head.appendChild(多个);
          elem.remove();
          / *变种F =新功能(code);
          F();*/
        }
      }
    };
  });}(角));

I'm using AngularJs with templating system. I want to add specific inline javascript script to each template adding alert box regards to the selected tab ( Home | List | Settings )

Html renders : but ng-scope is added and nothing alerts when you change of tabs.

<script type="text/javascript" class="ng-scope">alert("home")</script>

I make the example available here :

http://bit.ly/HWcN1H

or here

plunkr example with alert("template1") present into template1.html but renders as

<script type="text/javascript" class="ng-scope">alert("template1")</script>

解决方案

I have improved endorama's solution at github

The same process.

  1. Create the angular-loadscript.js (from the link above)
  2. in your app use 'ngLoadScript' as a resource dependency.

    var app = angular.module('YOUR_APP_NAME', ['ngResource','ngRoute', ...,'ngLoadScript']);

  3. In your partial use 'text/javascript-lazy' as the MIME type.

Everything should work as required:

/*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));

这篇关于为什么NG-范围添加到我的局部视图中的javascript内联,使警报不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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