ng-auto-save
Set of angularjs directives to save inputs' values as user types
Installation:
via NPM: npm install ng-auto-save
Reference module in your app
angular;
Demo:
Plunker Live Demo - Exchange with the database is imitated.
Example:
All you have to do in the controller is to provide a function to save record. This must return a promise and it takes three arguments:
field
- corresponds to the column in the table in database that will be updatedval
- new value to savekey
- the constraint that usually goes in the thewhere
clause of the sql update statement
index.js - inside Controller:
$scope { return $http ;};
index.html: Example uses angular-material, ng-messages, font-awesome
Name Name is required
Explanation of directives used:
auto-save="updateField"
- must be applied to the<form>
. It takes the name of the function used to save record.auto-save-key="article.id"
-article.id
here is the primary key in the table Articles. If it is undefined, theauto-save
function will not execute (thus making it easy to customize insert functionality).auto-save-debounce="1000"
- specify how long to wait for input before savingauto-save-field="name"
- must be applied to an input with attributeng-model
. The "name" here is the name of the column in the table to be updated.auto-saving="name"
- apply this directive to an element that you want to show up when saving is in progress. The value of the attribute must be set to the value of 'auto-save-field' applied to the corresponding inputauto-saved="name"
- apply this directive to an element that you want to show up when saving is in complete successfully. The value of the attribute must be set to the value of 'auto-save-field' applied to the corresponding input
Server side implementation Example for NodeJs as a web server using express.js and PostgreSql as a database.
Set Route:
router;
Implementation for route:
var query = ;var squel = ;squel;var sqlOptions = autoQuoteAliasNames: false ;queryconnectionParameters = 'postgres://postgres:mypassword@localhost:5432/Articles'; // example connection exports { var id = ; if id var sql = squel ; ; else resstatus500; };
Credits
Inspired by an article written by Adam Albrecht: "How to Auto-Save your model in Angular.js using $watch and a Debounce function."