A cleaner way to keep your standard JS objects in camelcase communicating to standardized snakecase APIs.
This angular module has no external dependencies, and can act automatically under $http
"with defaults transFormRequest
and transformResponse
" or can be used as $filter('snake')
or $filter('camel')
anywhere in your view or controller.
The easiest way to install is using bower
bower install --save ng-snake-camel
Alternatively you can download from the GitHub project: https://github.com/Serrabits/ngSnakeCamel
Load the ng-snake-camel.js
file into your web app after loading angular.js
<html>
...
<head>
...
<script src="angular.js"></script>
<script src="bower_components/ngSnakeCamel/ng-snake-camel.js"></script>
...
</head>
...
</html>
Make sure that your AngularJS application references the ngSnakeCamel
module:
angular.module('myApp', ['ngSnakeCamel']);
If you want to enable the transformation to all your requests $ http, set true in snakeCamelProvider.setHttpTransform(true)
like this:
angular.module ('myApp' ['ngSnakeCamel'])
.config(function(snakeCamelProvider) {
snakeCamelProvider.setHttpTransform(true);
});
To apply the transformation request by request, set the $http configs transformRequest
and transformResponse
as follows:
$http.post(http://example.com, {
transformRequest: function(data) {
return $filter('snake')(angular.toJson(data));
},
transformResponse: function(data) {
return $filter('camel')(angular.fromJson(data));
}
})
.success()
.error();
Note: angular.toJson()
and angular.fromJson()
are needed for when the transformation is applied directly to request the default transformations are not performed. For questions of a look at the $http documentation
To use the snake and camel filters in your views, simply apply within the braces
{{expression | snake}}
or {{expression | camel}}
. For questions of a look at the $filter documentation