简体   繁体   English

AngularJS指令呈现跳动

[英]AngularJS directive rendering is jumpy

I'm using AngularJS to create my site. 我正在使用AngularJS创建我的网站。

I've applied the ng-cloak directive over the entire body. 我已经在整个身体上应用了ng-cloak指令。

However, when loading, the elements appear to load asynchronously, (for example, when loading directive templates), causing elements to move around until the full page is loaded. 但是,在加载时,元素似乎是异步加载的(例如,在加载指令模板时),从而导致元素四处移动,直到加载整个页面为止。

How do I avoid this brief flash of elements moving around? 如何避免元素短暂闪烁? I've already applied ng-cloak. 我已经应用了ng-cloak。 Is there a better way to do this? 有一个更好的方法吗?

It seems that part of the problem is that the CSS styles are not applied immediately. 似乎部分问题是CSS样式没有立即应用。 For example, if I set the width of something that is slow to load, then it sometimes takes a brief millisecond before that style is applied to the element. 例如,如果我设置了加载速度较慢的东西的宽度,那么在将该样式应用于元素之前,有时会花费一毫秒。

You should concatenate your templates into your index.html file. 您应该将模板连接到index.html文件中。 Loading them asynchronously only creates unnecessary latency, slows down your website and ruins user experience. 异步加载它们只会造成不必要的延迟,降低网站速度并破坏用户体验。

That doesn't mean however that you should write all the templates in your index file, that would be nightmare to maintain. 但是,这并不意味着您应该在索引文件中编写所有模板,这将是噩梦。 Use a task processor like grunt or gulp to merge all your individual templates into index.html before deploying. 在部署之前,请使用任务处理器(例如gruntgulp)将所有单独的模板合并到index.html

The templates need to be within the ng-app element, inside script tags, like this: 模板需要在ng-app元素内的script标签内,如下所示:

<div ng-app="myApp">
    <div my-directive></div>

    <script type="text/ng-template" id="/my-directive.html">
        Content of the template.
    </script>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM