ng-submission
v1.0.2
Published
Keep track of pending/error states in Angular
Downloads
2
Maintainers
Readme
ng-submission
Keep track of pending/error states in Angular
Inspired by bendrucker/submission.
Install
$ npm install --save ng-submission
Usage
angular.module('myapp', [
require('ng-submission')
])
.controller('MyController', MyController)
function MyController ($http, ngSubmission) {
var self = this
var submission = ngSubmission()
this.postForm = function (data) {
console.log(submission) // => {pending: false, error: null}
const promise = submission.submit($http.post('/submit', {data: data}))
console.log(submission) // => {pending: true, error: null}
return promise
.then(function (response) {
console.log(submission) // => {pending: false, error: null}
})
.catch(function (error) {
console.log(submission) // => {pending: false, error: error}
})
}
}
Setup
Include 'ng-submission' in your angular module's dependencies:
// node module exports the string 'ng-submission' for convenience
angular.module('myApp', [
require('ng-submission')
])
// otherwise, include the code first then the module name
angular.module('myApp', [
'ng-submission'
])
API
service: ngSubmission
: function ()
-> submission
Exposes the 'ngSubmission' service, which is a function that returns a new submission
instance.
submission
Is an object, with three properties:
submission.submit: function(promise)
- A function that takes a promise and starts a new submission.submission.pending: Boolean
- whether the submit is pendingsubmission.error: Object|null
- if the previous submit errored, the error object from that submission. Otherwise, null.
License
MIT © Andrew Joslin