简体   繁体   English

Angular.js-在重定向期间保留输入的表单数据,以便用户在返回该表单时继续

[英]Angularjs - Retain the entered form data during redirection for the user to continue when coming back to that form

I am creating a simple user data collection project which requires the user to enter data in multiple forms. 我正在创建一个简单的用户数据收集项目,该项目要求用户以多种形式输入数据。

I have created separate html page for each form. 我为每种形式创建了单独的html页面。

There are forms for the user to enter personal details, educational details and a few more. 用户可以通过表格输入个人详细信息,教育详细信息等。

When a user enters personal details and clicks next I store the data in localstorage and in the end all the data are sent to db. 当用户输入个人详细信息并单击next我将数据存储在localstorage中,最后所有数据都发送到db。

When the user clicks next and again comes to the same page i retrieve the data from localstorage and bind it in the respective text boxes. 当用户单击next并再次进入同一页面时,我从localstorage检索数据并将其绑定在相应的文本框中。 But when the user does not click next and goes to another page by using the nav bar options the entered data is lost. 但是,如果用户没有单击next ,而是使用导航栏选项转到另一个页面,则输入的数据将丢失。

But I want the to data to be there even if the user does not click next and go to another page and come back to that page so that he can continue to fill the form where he left. 但是,我希望即使用户不单击next转到另一个页面然后返回该页面,该数据也仍然存在,以便他可以继续填写他离开的表单。

I am doing this project in Angularjs. 我正在Angularjs中执行此项目。

I have got no clue on how to do this, someone pls put me on the right track. 我对如何做到这一点一无所知,有人请让我走上正确的道路。

angular.module('userDetails').factory('sessionService', [function () {
return{
    getGlobal: function(key){
        var data = localStorage.getItem("user."+ key);
        if(data){
            return JSON.parse(data).data;
        }
        return null;
    },
    get : function(key){
        var data = localStorage.getItem("user."+ key);
        if(data){
            return JSON.parse(data).data;
        }
        return null;
    },
    set: function(key, value){
        localStorage.setItem("user."+key, JSON.stringify({data: value}));
    }
};

}]);

In my html 在我的HTML中

<button ng-click=savePD(PD)> Next </button>

When the next button is clicked in personal details form: 当单击“个人详细信息”表单中的next按钮时:

$scope.savePD = function(PD){
    sessionService.set("personalData",PD);
    $location.path('/educationData');
}

So when the next button is clicked the model values are passed to the function and then stored in local. 因此,当单击next按钮时,模型值将传递到函数,然后存储在本地。

So I need to know when I don't click next and go to another page how do i retain the currently entered value in the first form. 因此,我需要知道何时不单击下一步并转到另一页,如何在第一种形式中保留当前输入的值。

you can use $rootscope if you do not want your data to be lost in the page navigation. 如果您不希望数据在页面导航中丢失,则可以使用$rootscope

The data in rootscope can be accessed through any controller, just inject the rootscope in the controller . 可以通过任何控制器访问rootscope的数据, just inject the rootscope in the controller

Root scope should be initialized in the app.run function. 根范围应在app.run函数中初始化。

angular.module('myApp', [])
.run(function($rootScope) {
    $rootScope.form_data = {}
})
.controller('myCtrl', function($scope, $rootScope) {

})
.controller('myCtrl2', function($scope, $rootScope) {

});

form_data is available in every form in any controller as a hash. form_data以任何形式在任何控制器中以哈希形式提供。

Form 1, 表格1

<div ng-controller="myCtrl">
  <form>
    First Name:<br>
    <input type="text" ng-model="form_data.firstName"><br>
    Last Name:<br>
    <input type="text" ng-model="form_data.lastName">
    <br><br>
    <button ng-click="next()">Next</button>
  </form>
</div>

Form 2, 表格2

<div ng-controller="myCtrl2">
  <form>
    Profile:<br>
    <input type="text" ng-model="form_data.profile"><br>
    Age:<br>
    <input type="number" ng-model="form_data.age">
    <br><br>
    <button ng-click="next()">Next</button>
  </form>
</div>

Now you can retrieve the values from the rootscope from the last controller and send it to db. 现在,您可以从最后一个控制器的rootscope检索值,并将其发送到db。

.controller('myCtrl3', function($scope, $rootScope) {
  var data = $rootScope.form_data

  // call http using a service or here itself and send data as a parameter
});

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

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