AngularJS - 在路由之间保存数据

tee*_*ink 23 angularjs

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

现在,我正在使用"服务"来保存数据,这是有效的.然而,问题是因为"服务"是单身,从表单导航,然后回到它,仍将具有来自先前不完整或废弃形式的所有数据.

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

谢谢,
Tee

cal*_*tie 29

在下列情况下,为什么不增强服务来处理删除存储的数据:

  • 用户无法完成表单并导航远离整个表单提交过程
  • 用户提交表单

基本上,您可以按如下方式增强服务:

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 = {};
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

您的控制器可以如下:

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
}]);
Run Code Online (Sandbox Code Playgroud)

另一种方法是resetData()在路由更改为不在表单应用程序中的内容时调用服务的功能.如果你有像Form控制器的父控制器之类的东西,那么在那个控制器中,你可以监视路线变化:

$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
 });
Run Code Online (Sandbox Code Playgroud)


emr*_*azi 2

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

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

第一条路线的第一个控制器,

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

第二条路线的第二个控制器,

app.controller("SecondPageController", function($scope,User){ 
    $scope.user = User;  
    $scope.user.age = 24;
});
Run Code Online (Sandbox Code Playgroud)

  • 这将是一个糟糕的解决方案。当用户尝试填写表单时,表单通常尚未包含数据。根据问题,当用户填写表单、提交表单然后返回再次填写同一表单时,不应向用户显示旧的表单数据。您的解决方案建议为表单字段设置默认值,但这不是用户所期望的。 (4认同)