简体   繁体   English

在单页应用程序中使用AngularJs UI Router和全局变量

[英]Using AngularJs UI Router and global variables in single page application

I am creating a hybrid quiz application with AngularJS. 我正在用AngularJS创建一个混合测验应用程序。 I have below pseudo code, 我有下面的伪代码,

  • index.html where user selects the quiz. index.html,用户在其中选择测验。
  • 2nd page using ui-view Quiz is rendered and user selected options are stored in a global array (Should I use global here ?) 使用ui-view测验的第二页已呈现,并且用户选择的选项存储在全局数组中(我应该在此处使用global吗?)
  • The values in array are compared with values in answer array (should answers array be global? This array is created by querying the database and on each new quiz this array has to change as this will select questions randomly.) 将数组中的值与答案数组中的值进行比较(答案数组应该是全局的吗?此数组是通过查询数据库创建的,并且在每次新测验中此数组都必须更改,因为这将随机选择问题。)
  • Will both the arrays be carried forward to the score view (when using them as local arrays or I need to to make them global or they will not be carried to next view?) 这两个数组都将被带入分数视图吗(当将它们用作局部数组时,或者我需要使它们成为全局数组时,还是将它们不带入下一个视图?)

I am new to single page application and UI-Router so don't know how does it all work. 我是单页应用程序和UI-Router的新手,所以不知道它们如何工作。

I don't think you should store user's selected options in Global variable because these aren't common variable to be used throughout the user session. 我认为您不应该将用户选择的选项存储在Global变量中,因为这些不是在整个用户会话中都可以使用的通用变量。 You can save it in database when user redirects to 2nd page and retrieve on score view screen. 当用户重定向到第二页并在分数视图屏幕上检索时,您可以将其保存在数据库中。 Also, You may need this information in future to retain the user's records. 另外,您将来可能需要此信息来保留用户的记录。

EDIT - In case you can not store data in database and storing data at client side is only option then local storage can be used. 编辑 -如果您不能将数据存储在数据库中,并且仅在客户端存储数据,则可以使用本地存储。

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

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