Jul*_*DES 5 partials angularjs angularjs-scope angularjs-view
我正在使用AngularJs和模板系统.我想为每个模板添加特定的内联javascript脚本,为选定的选项卡添加警告框(主页|列表|设置)
Html呈现: 但是添加了ng-scope,并且在更改选项卡时没有任何警报.
<script type="text/javascript" class="ng-scope">alert("home")</script>
Run Code Online (Sandbox Code Playgroud)
我在这里提供示例:
或者在这里
带有alert("template1")的plunkr示例存在于template1.html中,但呈现为
<script type="text/javascript" class="ng-scope">alert("template1")</script>
Run Code Online (Sandbox Code Playgroud)
小智 12
我在github改进了endorama的解决方案
同样的过程.
在您的应用中使用"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));
Run Code Online (Sandbox Code Playgroud)