简体   繁体   English

移至AngularJS上的其他路线时如何保存输入数据?

[英]How can I keep my input data when moving to other routes on AngularJS?

I have an AngularJS application which is dynamically created. 我有一个动态创建的AngularJS应用程序。

Because of that, it may have a normal <form> with a submit button, but inside of it there could be a link to another one, and another one and so on. 因此,它可能具有带有提交按钮的常规<form> ,但是在其中可能有一个指向另一个的链接,另一个指向另一个,依此类推。

I've already accomplished to correctly submit the data no matter how deep in this "form-net" the user is, but the problem is that the user might not fill them in the "correct order", and the user is freely to do so. 无论用户在此“表单网络”中有多深,我都已经完成了正确提交数据的操作,但是问题是用户可能无法按“正确的顺序”填写数据,并且用户可以自由地进行操作所以。

For example, he/she could start filling FORM A and then realizing that there is FORM B inside of it, move to another route, filling and submitting FORM B and when coming back and FORM A it will be empty. 例如,他/她可以开始填写FORM A ,然后意识到其中有FORM B ,然后转移到另一条路线,填写并提交FORM B然后返回并返回FORM A时将为空。

So, I want to pre-save all input data the user filled. 因此,我想预先保存用户填写的所有输入数据。 I just want some sort of cache of any input the user might give, so the user can move freely from one form to another without an obligation to submit. 我只希望用户可以对任何输入进行某种形式的缓存,以便用户可以自由地从一种形式移动到另一种形式,而无需提交。

You could try using an Angular service and inject it as a dependency in all the controllers that the forms use. 您可以尝试使用Angular服务,并将其作为依赖项注入到表单使用的所有控制器中。 You would then bind the data to this service rather than to the controller. 然后,您将数据绑定到该服务而不是控制器。 Since the service is a singleton, it will only be created once and will not get recreated as the user moves throughout your app. 由于该服务是单例服务,因此它将仅创建一次,并且不会随着用户在整个应用程序中移动而重新创建。 The data bound values will persist as long as you don't change them and the user stays in the app. 只要您不更改数据绑定值,并且用户停留在应用程序中,数据绑定值将一直存在。

The service could look something like this: 该服务可能如下所示:

app.service('MyService', function () {
  return {
    form1Data: {},
    form2Data: {},
    form3Data: {}
  };
});

And then bind to the correct formNData field in your view layer. 然后绑定到视图层中正确的formNData字段。 If the number of forms also ends up being dynamic, you can add a configuration block to your Angular app that indicates how many of these form data object you'll need and use that inside the service function. 如果表单的数量最终还是动态的,您可以在Angular应用程序中添加一个配置块,以指示您需要多少个form数据对象,并在service函数中使用它。

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

相关问题 如何防止我的菜单移动页面中的其他元素? - How can I keep my menu from moving other elements in the page? 如何使用AngularJS路由“解析”获取$ scope的数据? - How can I use “resolve” with AngularJS routes to get data for my $scope? 更改路线时如何保留数据? - How do I keep the data when I change routes? 当我移动它时,如何获取范围输入的当前值并在控制台中打印? - How can i get the current value of my range input and print in console when i am moving it? 如何将按钮/输入保持在图像下方? - How can I keep my buttons/input underneath my image? 如何编辑动态添加的输入,而我的输入数据不会填充该数组中的其他输入? - How can I edit an input added dynamically, without my input data populating other inputs in that array? 如何防止光标/插入符号在输入文本元素内移动? - How do I keep my cursor/caret from moving inside of an input text element? 如何禁用AngularJS输入中的复选框? - How can I disable my checkbox from AngularJS input? 当用户调整窗口大小时,如何保持内容移动? - How do I keep my content moving when the user resizes their window? 如何防止我的内容区域移动 - How Do I Keep My Content Area From Moving
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM