在完成angular渲染后运行js脚本

Run js script after angular is done rendering

本文关键字:运行 js 脚本 angular      更新时间:2023-09-26

嗨,在完成角度渲染后,我一直在运行js脚本。我从克隆了代码https://github.com/codrops/AnimatedHeader

我需要它在html渲染后运行,因为它需要header元素,但由于我正在开发angularJs应用程序,渲染需要一些时间,所以我的脚本渲染得更早,给了我错误。所以我有办法让它等待。

我已经尝试过使用自定义指令,使用ng斗篷,将我的js放在window.onload中。我尝试过很多解决方案,但都不适用。

这是我的索引.html:

<html lang="en" ng-app="pliro" ng-cloak="">
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.2/angular-animate.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.8/angular-ui-router.min.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css
    </head>
    <body id="page-top" class="index">
        <script src="scripts/main/main.js"></script>
        <script src="scripts/main/main-controller.js"></script>
        <script src="scripts/header/header-controller.js"></script>
        <script src="scripts/footer/footer-controller.js"></script>
        </script>
        <div ng-include="'scripts/header/header.html'"></div>
        <div ui-view></div>
        <div ng-include="'scripts/footer/footer.html'"></div>
    </body>
    <script ng-src="js/classie.js"></script>
    <script ng-src="js/cbpAnimatedHeader.js"></script>
</html>

我需要classie.js和cbpAnimatedHeader.js在header.html渲染后运行。如有任何帮助,我们将不胜感激。

这两个js小于100行。我建议你把它重写成指令或服务。他们不会为你花费超过一天的时间。

在正文末尾加载所有js。添加<script>并循环检查是否已加载"header.html",然后本地加载其他js。我相信这比上面的选项要复杂得多。