Controller to POST JSON message


#1

I gathered form data from 3 different controller and shared the data using a factory.


.factory('Data', function () {
    return {
      type: '',
      location: '',
      startDate: '',
      endDate: '',
      notes: '',
      repeat: ''};
})

on the last page i want to be able to post using JSON.

since the

<form></form>

IS NOT present and i can’t use the “submit” function (or can I?).

What should my controller look like? (ng-click=“postToJSON()”)??

Below insert From: Ionic Tutorial on backend


angular.module('ionicApp', ['ionic', 'ngResource'])

.factory('Post', function($resource) {
  return $resource('/api/post/:id');
})

.controller('MainCtrl', function($scope, Post) {
  // Get all posts
  $scope.posts = Post.query();

  // Our form data for creating a new post with ng-model
  $scope.postData = {};
  $scope.newPost = function() {
    var post = new Post($scope.postData);
    post.$save();
  }
});

#2

The important thing here is to add ng-model to your input fields

<form ng-submit="newPost()">
  <label>Type</label>
  <input type='text' ng-model="postData.type/>
  <label>Location</label>
  <input type='text' ng-model="postData.location/>
  ....
  <button type="submit" class="button button-positive button-block">Create</button>
</form>

With input fields using ng-model=‘postData.*’ you define that values in your input fields will to properties of postData object in $scope.

Here I used a submit button and ng-submit on form to specify the method of the controller to call but you could also not use ng-submit and add a button or a link with ng-click=‘newPost()’.

https://docs.angularjs.org/guide/forms


#3

you can $http to refactory with data json, i thin best than resource.