AngularJS - 节能路线之间的数据 [英] AngularJS - saving data between routes

查看:89
本文介绍了AngularJS - 节能路线之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是保存路线之间数据的最佳方式?结果
所以我有一个带有4个步骤的形式,我想从可结转至下一步骤previous一步的数据。

What would be the best way to save data between routes?
So I have a form that has 4 steps and I would like for the data from a previous step to be carried over to the next step.

现在,我使用的是服务来保存数据,其工作方式。然而,问题是,因为服务是单身,从形式上导航走,回来吧,仍将具有从previous所有的数据不完整的或废弃的形式。

Right now, I'm using a "service" to save the data, which works. However, the problem is since a "service" is singleton, navigating away from the form, and coming back to it, will still has all the data from a previous incomplete or abandoned form.

有没有解决这种情况的好办法?

Is there a good way to solve this kind of situation?

感谢你,结果
T恤

Thank you,
Tee

推荐答案

你为什么不提升服务才能删除存储的数据的照顾时:

Why don't you enhance the service to take care of deleting the stored data when:


  • 的用户不填写表格,并从整个表单提交过程
  • 导航离开
  • 的用户提交表单

基本上,你可以提高你的服务如下:

Basically, you could enhance your service as follows:

myApp.factory('myService', function () {
    var formData = {};

    return {
        getData: function () {
            //You could also return specific attribute of the form data instead
            //of the entire data
            return formData;
        },
        setData: function (newFormData) {
            //You could also set specific attribute of the form data instead
            formData = newFormData
        },
        resetData: function () {
            //To be called when the data stored needs to be discarded
            formData = {};
        }
    };
});

然后,您可以控制​​如下:

Your controller could then be as follows:

myApp.controller('FirstStepCtrl', ['$scope', 'myService', function ($scope, myService) {
    //This is the controller of the first step in the form
    //Reset the data to discard the data of the previous form submission
    myService.resetData();

    //Remaining Logic here
}]);

myApp.controller('LastStepCtrl', ['$scope', 'myService', function ($scope, myService) {
    //This is the controller of the last step in the form

    //Submits the form
    $scope.submitForm = function () {
        //Code to submit the form

        //Reset the data before changing the route - assuming successful submission
        myService.resetData();

        //Change the route
    };

    //Remaining Logic here
}]);

另一种方法是调用服务的 resetData()功能时的路由变化的东西,不是窗体应用程序中。如果你有类似父控制器的形式步进控制器,然后在控制器,你可以保持一个看守路由变化:

Another alternative is to call the service's resetData() function when the route changes to something that is not within the form application. If you have something like a Parent Controller for the Form step controllers, then in that controller, you could keep a watch over the route change:

$scope.$on('$routeChangeStart', function() { 
   //Use $location.path() to get the current route and check if the route is within the 
   //form application. If it is, then ignore, else call myService.resetData()

   //This way, the data in the forms is still retained as long as the user is still
   //filling up the form. The moment the user moves away from the form submission process,
   //for example, when explicitly navigating away or when submitting the form, 
   //the data is lost and no longer available the next time the form is accessed
 });

这篇关于AngularJS - 节能路线之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆