How to implement a directive that auto-saves form data periodically

0 votes
Can i know How to implement a directive that auto-saves form data periodically?
4 days ago in Node-js by Nidhi
• 14,600 points
28 views

1 answer to this question.

0 votes

To create a directive that automatically saves form data at intervals in AngularJS, utilize $interval within a custom directive to monitor the form model and initiate a save function consistently.

AngularJS Auto-Save Directive

1. Directive Code

app.directive('autoSave', function($interval) {

  return {

    restrict: 'A',

    scope: {

      model: '=autoSave',

      onSave: '&'

    },

    link: function(scope) {

      var intervalPromise = $interval(function() {

        if (scope.model) {

          scope.onSave({ data: angular.copy(scope.model) });

        }

      }, 5000); // auto-save every 5 seconds

      scope.$on('$destroy', function() {

        $interval.cancel(intervalPromise);

      });

    }

  };

});

2. Usage in HTML

<form name="userForm">

  <input type="text" ng-model="user.name" placeholder="Name" />

  <input type="email" ng-model="user.email" placeholder="Email" />

  <!-- Auto-save directive usage -->

  <div auto-save="user" on-save="saveUser(data)"></div>

</form>

3. Controller Function

$scope.user = {};

$scope.saveUser = function(data) {

  console.log("Auto-saving data:", data);

  // You can replace this with an API call

};

answered 3 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

run npm run script1 it works for me READ MORE

answered Jan 10, 2023 in Node-js by Harisudarsan

edited Mar 5 42,043 views
0 votes
1 answer

How to get data out of a Node.js http get request?

Hello @kartik, Your logs return undefined : you log before ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,840 points
7,242 views
0 votes
1 answer

How to implement a writable stream?

Hello @kartik, to create a writeable stream is ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,840 points
1,307 views
0 votes
1 answer

How to handle the swiperight event to trigger custom actions in jQuery Mobile?

To handle the swiperight event and trigger ...READ MORE

answered 3 days ago in Node-js by anonymous
30 views
0 votes
1 answer
0 votes
1 answer

How to develop a directive that restricts user input based on custom conditions?

To create an Angular directive that restricts ...READ MORE

answered 3 days ago in Node-js by anonymous
34 views
0 votes
1 answer

How to dynamically inject components into the DOM using @Component?

You need to use ComponentFactoryResolver and ViewContainerRef. ...READ MORE

answered 3 days ago in Node-js by anonymous
40 views
0 votes
1 answer

How to implement a directive that toggles element visibility with animations?

Custom Directive with Angular Animations 1. Define the ...READ MORE

answered 3 days ago in Node-js by anonymous
36 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP