gpt4 book ai didi

javascript - Angular js - 使用延迟加载从 View 中需要 js 文件

转载 作者:行者123 更新时间:2023-12-03 12:38:39 25 4
gpt4 key购买 nike

如何在延迟加载中包含多个 js 文件,以便当页面完成加载并且 Angular 已执行时?

我尝试使用自定义指令,代码如下所示://html

<ng-lazy-load data-type="script" data-src='["file.js"]'></ng-lazy-load>

//指令

.directive('ngLazyLoad', [function () {
return {
restrict: 'E',
templateUrl:'views/modules/module_lazy_load.html',
scope:true,
link: function ($scope, element, attr) {
if(attr.type === 'script'){
$scope.src = JSON.parse(attr.src);
}

}
};
}]);

//指令 View

<script ng-if="src" ng-repeat="path in src" src="{{path}}"></script>

//file.js 包含在延迟加载中

alert('daf***');

最佳答案

我不这么认为,因为脚本是在 Angular 接管之前加载的。

一个可以检查你的内容的指令怎么样,然后你可以将脚本 $document.write 写入index.html?

将负责作用域变量的 Controller 注入(inject)到指令中,然后它就可以有条件地写入html。

关于javascript - Angular js - 使用延迟加载从 View 中需要 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23611029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com