Skip to content

Instantly share code, notes, and snippets.

@stonly
Created November 19, 2013 19:07
Show Gist options
  • Save stonly/7550703 to your computer and use it in GitHub Desktop.
Save stonly/7550703 to your computer and use it in GitHub Desktop.
strictHttp is an alternate $http.post service that checks for values before making the $http call.
var app = angular.module('app',[]);
app.factory('strictHttp', function ($http) {
return {
post: function(url, params, req, config){
var invalid;
for(var p in params){
if(req.indexOf(p) > -1 && (params[p] === '' || params[p] === undefined)){
invalid = 1;
}
}
if(!invalid){
return $http.post(url, params, config);
}
return {
success : function(f){
f('Request Failed');
}
};
}
}
});
app.controller('Controller1',function($scope, strictHttp){
var params = {}
params.first = 'test';
params.second = '';
strictHttp('http://apiurl.io', params, ['first', 'second'], {}).success(function(data){
console.log(data)
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment