Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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 如何编译angular指令创建的angular脚本代码_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs 如何编译angular指令创建的angular脚本代码

Angularjs 如何编译angular指令创建的angular脚本代码,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我刚刚创建了一个创建脚本文件的指令,并将该文件附加到html主体元素中 但角度代码在该文件中不起作用。如果我写的java脚本代码运行良好 application.directive("dynamicScript", function(service,$compile,$http,$rootScope){ return { restrict:"E", terminal: true, scope:{ }, l

我刚刚创建了一个创建脚本文件的指令,并将该文件附加到html主体元素中

但角度代码在该文件中不起作用。如果我写的java脚本代码运行良好

application.directive("dynamicScript",
function(service,$compile,$http,$rootScope){
    return {
        restrict:"E",
         terminal: true,
        scope:{

        },
        link:function(scope,element,attrs){

            //scope.scriptname = attrs.name;
            //service.setData(attrs.name);

            //$http.get('generatescript').then(function(response) {
                var scriptElem = angular.element(document.createElement('script'))
                scriptElem.attr("src", "./js/SAMPLEScript.js")
                scriptElem.attr("type", "text/javascript")
                //element.html('').append( $compile( scriptElem.contents() )( $rootScope ) );
                var ele = angular.element(document.getElementsByTagName("body")).append(scriptElem);
                $compile(scriptElem.contents())(scope);

            //});

        }
    }
});

SAMPLEScript.js包含不起作用的角度脚本代码。如何当前加载此文件。

您可以创建如Angular jar文档所示的指令。这将有助于避免显式编译,您也可以直接使用编译函数。
$compile

尝试过,但不起作用。我没有创建脚本代码,而是将脚本文件包含在响应html文件中,然后脚本代码也不起作用。如果文件包含纯java脚本代码,则其工作正常
我使用$compile编译了响应代码,然后编写了angular指令,该指令在响应html加载中编写得非常完美,但脚本代码有问题。