2013-05-28 6 views
23

मार्गों के बीच डेटा को सहेजने का सबसे अच्छा तरीका क्या होगा?
तो मेरे पास एक ऐसा फॉर्म है जिसमें 4 कदम हैं और मैं पिछले चरण से डेटा को अगले चरण तक ले जाना चाहता हूं।कोणीयजेएस - मार्गों के बीच डेटा सहेजना

अभी, मैं डेटा को सहेजने के लिए "सेवा" का उपयोग कर रहा हूं, जो काम करता है। हालांकि, समस्या यह है कि एक "सेवा" सिंगलटन है, फॉर्म से दूर नेविगेट कर रही है, और उस पर वापस आ रही है, फिर भी पिछले अपूर्ण या त्याग किए गए फॉर्म से सभी डेटा होगा।

क्या इस तरह की स्थिति को हल करने का कोई अच्छा तरीका है?

धन्यवाद,
टी

+1

के संभावित डुप्लिकेट [AngularJS, मार्गों के बीच गुंजाइश गुजर] (http://stackoverflow.com/questions/13882077/angularjs-passing-scope-between-routes) –

उत्तर

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; 
}); 
+4

यह एक खराब समाधान होगा। जब उपयोगकर्ता उन्हें भरने का प्रयास करता है तो आमतौर पर फॉर्म में पहले से डेटा नहीं होता है। प्रश्न के मुताबिक, जब कोई उपयोगकर्ता फॉर्म भरता है, तो फॉर्म जमा करता है और फिर उसी फॉर्म को फिर से भरने के लिए वापस आता है, उपयोगकर्ता को पुराने फॉर्म डेटा के साथ प्रस्तुत नहीं किया जाना चाहिए। आपके समाधान में फॉर्म फ़ील्ड के लिए डिफ़ॉल्ट मान होने का प्रस्ताव है जो उपयोगकर्ता को यहां अपेक्षा नहीं करता है। – callmekatootie

+0

इसके अलावा, जब आप दृश्य से डेटा मैप करने के बजाय सीधे नियंत्रक में डेटा को ओवरराइड करने की योजना बनाते हैं तो सेवा क्यों कॉल करें? – callmekatootie

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

आपका नियंत्रक तो इस प्रकार हो सकता है:

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 
}); 
+0

धन्यवाद @callmekatootie। हां, रीसेटडेटा फ़ंक्शन होने पर वर्तमान में मेरे मन में क्या है।मुझे लगता है कि यह कुछ हद तक हैकी है कि मुझे पथों का ट्रैक रखने की जरूरत है। मुझे लगता है कि कोणीय एक डबल टेम्पलेट सम्मिलन करने में सक्षम होगा, यानी अगर आपके पास एचटीएमएल टैग में नियंत्रक है, तो यह मार्ग के बावजूद जारी रहता है। तो इस मामले में, मेरे पास एक अभिभावक नियंत्रक है जो मार्ग परिवर्तन के समय पुनः लोड नहीं किया जाएगा, लेकिन उनके पास प्रत्येक चरण के लिए उप नियंत्रक भी है। मुझे नहीं पता कि यह करना संभव है या नहीं। – teepusink

संबंधित मुद्दे