有没有一种优雅的方法可以将javascript文件包含在自定义指令javascript文件中

Is there a elegant way to include javascript file in custom directive javascript file?

本文关键字:javascript 文件包 自定义 文件 指令 一种 方法 有没有      更新时间:2023-09-26

自定义指令<my-directive-a>使用另一个自定义指令<my-directive-b>在main.html中,只有<my-directive-a>使用。但我应该在main.html中包含myDirectiveB.js(定义<my-directive-b>)。有没有一种优雅的方法可以删除它?我想在myDirectiveA.js中包含myDirectiveB.js,而不是在main.html中。

main.html

<html>
<body>
    <my-directive-a>
    </my-directive-a>
    <script src='/angularjs/shared/myDirectiveA.js'></script>
    <!-- I want to remove following one line -->
    <script src='/angularjs/shared/myDirectiveB.js'></script>
</body>
</html>

myDirectiveA.js

...
.directive("myDirectiveA", function() {
    return {
        restrict: 'E',           
        template: '<div><my-directive-b></my-directive-b></div>' 
    }
});

myDirectiveB.js

...
.directive("myDirectiveB", function() {
    return {
        restrict: 'E',           
        template: '<div>my directive b</div>' 
    }
});

似乎您正在寻找类似@import的东西,它在css中可用。但不幸的是,不可用

但是你正在使用jQuery可以尝试

$.getScript('fileName',function(){
 //Rest of Code
})

否则使用纯JS

var crScript = document.createElement('script');
   crScript.src ="file Location";
   document.whereYouWantToAppend.apendChild(crScript);