简体   繁体   English

有角度的初始数据加载,将指令推迟到初始化之后

[英]Angular initial data load, defer directives until after initialise

I need to load some data into my angular app the first time it is loaded. 我需要在首次加载我的角度应用程序时加载一些数据。 The data needs to come from the server. 数据需要来自服务器。

My app currently consists of a number of directives on a single view in an accordion. 我的应用程序当前在手风琴的单个视图上包含许多指令。 The directives are forms whose criteria needs to be met before the user can progress to the next accordion group. 这些指令是在用户进入下一个手风琴组之前需要满足其条件的表格。 The first accordion group needs to use the data from the server to display to the user, there is a dependency on the data for the view. 第一个手风琴组需要使用来自服务器的数据来显示给用户,视图依赖于数据。

What is the best way to inject this data in the first instance? 首先插入数据的最佳方法是什么?

I could move the initial data call into the first directive but it doesn't really feel like it is 'its' job. 我可以将初始数据调用移到第一个指令中,但实际上并没有感觉到这是“工作”。 I could create another directive to handle this load but then I would need manage the load order of the directives. 我可以创建另一个指令来处理此负载,但随后需要管理指令的负载顺序。 I have had a look at ng-init which works well until the server call is made, during this call the directives run before it completes. 我看了ng-init,它在服务器调用之前一直有效,在此调用期间,指令在完成之前运行。

Any of the above would work for me in theory but i'm not sure that they are the best approach. 从理论上讲,以上任何一种方法都对我有用,但是我不确定它们是否是最佳方法。 What would you suggest is the best approach? 您认为最好的方法是什么?

Thanks 谢谢

You should use a resolve in your state, so, both views and controllers are called when the resolution is done. 您应该在您的状态下使用resolve,因此,解析完成后,将同时调用视图和控制器。

have a look at this link: http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx 看看这个链接: http : //odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx

您可以使用ng-if属性修饰指令,并作为值传递一个范围属性,该范围属性表示数据加载前。

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

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