简体   繁体   English

在AngularJS中异步加载数据的正确方法

[英]Proper way to asynchronously load data in AngularJS

Let us consider the following scenario: some data (in JSON format) needs to be shown to the end user. 让我们考虑以下情形:一些数据(JSON格式)需要显示给最终用户。 The usual way of doing this (or at least my own way) is to use an Angular factory to return the data, include the factory in the controller and display the data using a loop statement / directive . 这样做的通常方法(或者至少是我自己的方法)是使用Angular 工厂返回数据,将工厂包括在控制器中,并使用loop语句/ 指令显示数据。

However, the data is growing bigger day by day (from dozens to thousands) and some fancy animations are added to the views causing the load times of the views to reach the sky. 但是,数据每天都在增长(从数十个增加到数千个),并且在视图中添加了一些精美的动画,从而导致视图的加载时间达到了天空。

Usually, there are many ways of loading data (eg lazy loading , $q service ), but I wonder which one is closer to be the most reliable in serving data asynchronously. 通常,有多种加载数据的方式(例如, 延迟加载$ q service ),但我想知道哪种方法更异步地最可靠地提供数据。

If I understand your question correctly your problem is that you have a ton of data to display, this can go wrong in 2 places: 如果我正确理解了您的问题,那么您的问题是您要显示大量数据,这可能会在两个地方出错:

Issue 1: Loading the data 问题1: 加载数据

In order to keep the loading quick you can build something with pagination , this will split the data up in smaller pages to keep the loadtimes quick. 为了保持快速加载,您可以使用分页进行构建,这会将数据分成较小的页面,以保持快速的加载时间。

Issue 2: $$watchers count 问题2: $$$$$$$$$$$$

When displaying a ng-repeat your $$watchers count can increase very quickly, this will slow down your $digest cycle drastically, making your UI unresponsive, in order to overcome this you can use something that uses virtualization , such as UI-grid , this will only render a part of the data on the screen untill scrolled and unrenders everything outside of your view. 当显示ng-repeat您的$$watchers计数可以非常迅速地增加,这会减慢你的$digest周期大幅,使您的用户界面反应迟钝,为了克服这一点,你可以使用的东西,使用virtualization ,如UI电网 ,这只会在屏幕上呈现部分数据,直到滚动并取消渲染视图之外的所有内容。

In addition to Martijn Welker answer, here are few more points which might help you. 除了Martijn Welker的答案以外,这里还有其他几点可能会有所帮助。

1.First thing, always get the big data after you have loaded the page. 1.首先,加载页面后始终获取大数据。 You can get small data like global stats when you are loading the page(using the resolve thing but the data in the table should always come after the page is loaded). 加载页面时,您可以获得诸如全局统计信息之类的小数据(使用解决方法,但表中的数据应始终在页面加载后出现)。 Give user a loading icon on table portion while you do this. 在执行此操作时,为用户提供表格部分的加载图标。

2.Do you directly show the data as JSON to the user (like a JSON file)? 2.您是否直接将数据显示为JSON(例如JSON文件)给用户? You can improve that by incrementally showing the data(something which JSONblob.com does). 您可以通过逐步显示数据(JSONblob.com所做的事情)来改善这一点。 It shows top level keys, on clicking a key, it gives next level keys. 它显示了顶级键,单击某个键后,会显示下一个键。 Your backend can also be designed in such a way that you can give different request for different level and keys. 您的后端也可以设计为可以针对不同的级别和密钥提出不同的请求。 Or you can have headers like: 或者,您可以具有以下标题:

user > profile > eduction

and show the data for this key, while making the headers clickable.You can also store previous details(small data) in case user wants to return back. 并显示该键的数据,同时使标题可单击。您还可以存储以前的详细信息(小数据),以防用户要返回。

3.Try using pagination(backend or frontend your choice). 3.尝试使用分页(后端或前端选择)。 If you are not doing any sorting/filtering stuff with your data and its readonly, you can use one way binding (Angular 1.3+) which removes the watches from your list. 如果您不对数据及其只读内容进行任何排序/过滤工作,则可以使用单向绑定(Angular 1.3+)将手表从列表中删除。 Even though, I don't know where you will need pagination for showing json :). 即使,我也不知道显示json :)时需要分页。 Other than that, you can use plainJs to create this list. 除此之外,您可以使用plainJs创建此列表。

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

相关问题 正确的方式加载angularJS - Proper way to load angularJS 如何使用带有SpringMVC的AngularJS异步加载数据? - How to load data asynchronously using AngularJS with SpringMVC? 异步加载网站不同部分的正确方法是什么? [等候接听] - What is the proper way to load different sections of my site asynchronously? [on hold] 在AngularJS应用程序的控制器超类中加载依赖项的正确方法是什么? - What is the proper way to load dependencies in a controller super class of an AngularJS application? 在 javascript 中异步调用多个承诺的正确方法 - Proper way of calling multiple promises asynchronously in javascript ReactJS - 从 redux 和加载表单中获取数据的正确方法 - ReactJS - Proper way to fetch data from redux and load form 在 React 中渲染之前等待 function 完成或加载数据的正确方法? - Proper way to wait for a function to finish or data to load before rendering in React? 异步数据加载后提交HTML表单的正确方法是什么? - What is the proper way to submit HTML form after asynchronous data load? AngularJS传递变量的正确方法 - AngularJS Proper way to pass variables 使用json数据异步填充AngularJS ngTable - asynchronously populating an AngularJS ngTable with json data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM