【问题标题】:AngularJS - saving data between routesAngularJS - 在路由之间保存数据
【发布时间】:2013-05-24 01:29:39
【问题描述】:

在路线之间保存数据的最佳方法是什么?
所以我有一个包含 4 个步骤的表单,我希望将上一步的数据转移到下一步。

现在,我正在使用“服务”来保存数据,这很有效。但是,问题在于,由于“服务”是单例的,因此在离开表单并返回表单时,仍将拥有来自先前不完整或废弃表单的所有数据。

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

谢谢你,
三通

【问题讨论】:

标签: angularjs


【解决方案1】:

为什么不增强服务以在以下情况下删除存储的数据:

  • 用户未完成表单并导航离开整个表单提交过程
  • 用户提交表单

基本上,您可以通过以下方式增强您的服务:

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 = {};
        }
    };
});

您的控制器可能如下所示:

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() 函数。如果您有类似表单步骤控制器的父控制器,那么在该控制器中,您可以监视路由更改:

$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
 });

【讨论】:

  • 谢谢@callmekatootie。是的,拥有一个 resetData 函数是我目前想到的。我只是觉得我需要跟踪路径有点笨拙。我在想 Angular 能够进行双模板插入,即如果您在 html 标记中有一个控制器,那么无论路由如何,它都会持续存在。所以在这种情况下,我有一个父控制器,当路由更改时不会重新加载,但它们也有每个步骤的子控制器。我只是不知道这是否可行。
【解决方案2】:

Services 将在您的应用程序的整个生命周期中持续存在。所以你不应该有任何问题。您需要做的就是创建一个Service 并将其注入需要访问它的控制器。比如下面我给控制器注入了User服务。

只需为每个路由使用不同的控制器,这样您在范围内的模型就不会被覆盖。

第一个路由的第一个控制器,

app.controller("FirstPageController", function($scope,User){   
    $scope.user = User;
    $scope.user.firstName  = "Emre";
    $scope.user.secondName = "Nevayeshirazi";
});

第二个路由的第二个控制器,

app.controller("SecondPageController", function($scope,User){ 
    $scope.user = User;  
    $scope.user.age = 24;
});

【讨论】:

  • 这将是一个糟糕的解决方案。当用户尝试填写表单时,表单通常不包含数据。根据问题,当用户填写表单,提交表单,然后再次填写相同的表单时,不应向用户呈现旧的表单数据。您的解决方案建议为表单字段设置默认值,这不是用户在这里所期望的。
  • 另外,当您打算直接覆盖控制器中的数据而不是从视图映射数据时,为什么要调用该服务?
猜你喜欢
  • 2015-08-31
  • 1970-01-01
  • 2016-01-08
  • 2016-06-06
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
相关资源
最近更新 更多