脚本未在 templateurl 中运行 [英] script not running in templateurl

查看:20
本文介绍了脚本未在 templateurl 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 Angular js 文件

test.js 文件:

var app = angular.module("angleapp", []).controller('MainController', ['$scope', function ($scope) {$scope.test = "大家好"}]).directive('programlisting', function () {返回 {限制:'E',范围: {},templateUrl: '/directives/test.html'};});

test.html 文件:

<html xmlns="http://www.w3.org/1999/xhtml"><头><script type="text/javascript">警报(停止");<身体>你好你好吗</html>

这是我的索引文件:

 <html xmlns="http://www.w3.org/1999/xhtml"><头><title></title><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script><script src="http://code.highcharts.com/highcharts.js"></script><script src="http://code.highcharts.com/modules/exporting.js"></script><script src="/directives/test.js"></script><body data-ng-app="angleapp"><div class="main" ng-controller="MainController"><节目单></节目单>

</html>

现在当我尝试运行这个

你好吗

正在输出中.但是警报窗口没有弹出我哪里出错了?

解决方案

问题是 script 标签在注入 innerHTML 时没有被浏览器自动执行(会发生什么在 templateUrl 的情况下).通常以编程方式评估此标记 (eval).jQuery 做到了,但 jqLit​​e(Angular 的内部类似 jQuery 的实现)没有.

在您的情况下,修复很简单,将 jquery 脚本标记 置于 angular 之前,然后 Angular 将使用 jQuery 进行 DOM 操作:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script><script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

另外,你需要清理你的 test.html 模板,只留下正文内容.这是 test.html 的样子:

你好吗<script type="text/javascript">警报(停止");

最后,我不知道为什么你需要在部分模板中使用 script 标签,但在大多数情况下,这是个坏主意,还有更好的方法.

This is my angular js file

test.js file:

var app = angular.module("angleapp", [])
.controller('MainController', ['$scope', function ($scope) {
    $scope.test = "hi all"
}])
.directive('programlisting', function () {
    return {
        restrict: 'E',
        scope: {
        },
        templateUrl: '/directives/test.html'
    };
});

test.html file:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript">
            alert("stop");
        </script>
    </head>
    <body>
        hi how are you
    </body>
</html>

This is my index file:

 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
        <script src="/directives/test.js"></script>
    </head>
    <body data-ng-app="angleapp"> 
        <div class="main" ng-controller="MainController">
            <programlisting></programlisting>
        </div>
    </body> 
</html>

Now when I try to run this

hi how are you 

is coming in the output. but the alert window does not pop-op where i am i going wrong?

解决方案

The problem is that script tags are not executed by browser automatically when injected with innerHTML (what happens in case of templateUrl). Normally this tags are evaluated programmatically (eval). jQuery does it but jqLite (internal jQuery-like implementation of Angular) does not.

In your case the fix is simple, move jquery script tag before angular, then Angular will use jQuery for DOM manipulations:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

Also, you need to clean up your test.html template, leave only body content. This is how test.html should look:

hi how are you

<script type="text/javascript">
    alert("stop");
</script>

And finally, I don't know why you need to have script tag inside of partial template, but in most cases this is bad idea, and there are better approaches.

这篇关于脚本未在 templateurl 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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