आप सेवा जब संग्रहीत डेटा को हटाने का ख्याल रखना क्यों बढ़ाने नहीं है:
- उपयोगकर्ता फॉर्म भरने और पूरे फॉर्म जमा करने की प्रक्रिया से दूर नेविगेट नहीं करता
- उपयोगकर्ता प्रपत्र सबमिट
असल में, इस प्रकार आप अपनी सेवा को बढ़ाने सकता है:
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
});
के संभावित डुप्लिकेट [AngularJS, मार्गों के बीच गुंजाइश गुजर] (http://stackoverflow.com/questions/13882077/angularjs-passing-scope-between-routes) –