我正在使用 AngularJs 和模板系统。
我想向每个模板添加特定的内联 javascript 脚本,添加与所选选项卡相关的警报框(主页 | 列表 | 设置)
Html 渲染:但是添加了 ng-scope,并且当您更改选项卡时没有任何警报。
<script type="text/javascript" class="ng-scope">alert("home")</script>
我在这里提供了这个例子:
http://bit.ly/HWcN1H http://bit.ly/HWcN1H
or here
笨蛋的例子 http://plnkr.co/edit/BVZt3gdEQc4DhFoM6CUg?p=preview警报(“template1”)出现在 template1.html 中,但呈现为
<script type="text/javascript" class="ng-scope">alert("template1")</script>
我改进了endorama的解决方案github https://gist.github.com/subudeepak/9617483#file-angular-loadscript-js
同样的过程。
- 创建 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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)