Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
angularjs包含模板不运行主javascript_Javascript_Html_Angularjs - Fatal编程技术网

angularjs包含模板不运行主javascript

angularjs包含模板不运行主javascript,javascript,html,angularjs,Javascript,Html,Angularjs,我在angularjs工作时注意到了一些事情。我有一个模板文件夹,它有一个名为menu.html的文件 menu.html文件包含html元素。我的主页包括处理menu.html元素的脚本代码 我在主页面中调用menu.html,如下所示: <div ng-include="'templates/menu.html'"></div> 但是javascript代码不起作用。如果我在menu.html中编写javascript代码,它就会工作。included menu

我在angularjs工作时注意到了一些事情。我有一个模板文件夹,它有一个名为menu.html的文件

menu.html文件包含html元素。我的主页包括处理menu.html元素的脚本代码

我在主页面中调用menu.html,如下所示:

<div ng-include="'templates/menu.html'"></div>


但是javascript代码不起作用。如果我在menu.html中编写javascript代码,它就会工作。included menu.html不适用于Jquery或其他引用。

AngularJS不会执行通过templateUrl(或template)指定的部分中包含的任何JavaScript代码

为了解决这个问题,我使用了一个指令来加载javascript

app.directive('script', function() {
     return {
        restrict: 'E',
        scope: false,
        link: function(scope, elem, attr) {
            if (attr.type === 'text/javascript-lazy') {
                var code = elem.text();
                /*jslint evil: true */
                var f = new Function(code);
                f();
            }
        }
    }
});
在您的menu.html上

<script type="text/javascript-lazy">
  // javascript code here
</script>

//这里是javascript代码

控制台中是否有任何错误?顺便说一下:这应该是(删除)模板中包含的javascript不会被解析。