How to post data values using json and saving it in a remote database


#1

Please tell me how to post the data from the app and string the values in remote server database.?

Thanks in advance


#2

If you have a RESTful API for your remote database, you should use ngResource: https://docs.angularjs.org/api/ngResource/service/$resource


#3

@nico can you provide me one example please ?

i have an api which handles the data and stores it in database.

thanks in advance.


#4

Install angular-resource with bower

in bower.json

 {
  "name": "HelloIonic",
  "private": "true",
  "devDependencies": {
     "ionic": "driftyco/ionic-bower#1.0.0-beta.11",
     "angular-resource": ""
  }
}

Then, don’t forget to add it in your index.html

 <script src="lib/angular-resource/angular-resource.js"></script>

You could now build a service like this:

angular.module('you_app.service.api', ['ngResource'])

.factory('ApiService', function($resource) {
    return $resource(server_url + '/your/restful/route/:id', {id: '@id'}, {
        update: {method: 'PATCH'}     
    });
});

In a controller you can now easily fetch data

ApiService.get().$promise.then(function(data) {
  $scope.you_data = data;
});

#5

@nico can you provide me a step by step tutorial or refer some in the same please.

Since i am new to angularJS i don’t know how to achieve it.

Thanks