预编译角度js模板以加速应用程序启动

Que*_*tin 5 javascript templates precompile angularjs

我有一个很大的角度应用程序,主页面中包含5个模板

 <script type="text/ng-template" id="/myMainTemplate.html">...</script>
 <script type="text/ng-template" id="/myTemplatePage2.html">...</script>
 <script type="text/ng-template" id="/myTemplatePage3.html">...</script>
 <script type="text/ng-template" id="/myTemplatePage4.html">...</script>
Run Code Online (Sandbox Code Playgroud)

但是,我的应用程序需要大量时间来启动.

删除模板2/3/4修复它但当然打破了应用程序,我的猜测是angularjs需要花费太多时间来编译5个模板.

有没有办法预编译angularjs模板,例如使用nodejs或类似的东西(我们可以用手把编译模板的方式相同)?

如果我理解该$compile指令,那么想法就是将指令移到$compile(myTemplate)客户端的服务器端

Jed*_*rds 8

看看grunt-html2js任务.

https://github.com/karlgoldstein/grunt-html2js

它将简单的Angular HTML模板转换为JavaScript,以便连接到主应用程序包中.一旦模板在您的应用程序包中,它们将在Angular的模板缓存中启动,以便立即加载.


Que*_*tin 4

看来我错了,需要时间的不是编译模板。浏览器加载所有页面的速度很慢,基本上 document.ready 事件需要一些时间才能触发。

为了解决这个问题,我只<script type="text/ng-template" id="/myMainTemplate.html">...</script>在页面中包含我的主模板,并根据此解决方案使用异步 $http 调用加载其他模板: https: //gist.github.com/vojtajina/3354046