如何将表单数据放入angularjs中的json文件中

时间:2015-10-25 11:07:13

标签: html angularjs

我正在创建一个应用程序,我必须从表单输入中添加联系人。我想将表单输入字段中的数据添加到我的JSON文件中。

不幸的是,到目前为止我无法找到任何解决方案。请在这方面提供帮助。这是我的控制器代码,其中还定义了JSON对象。我想从输入字段中获取数据到JSON对象数组。

var contactManager = angular.module('contactManager', ['ngAnimate']);

// Contacts List Controller
contactManager.controller('ListController', ['$scope', '$http', function($scope, $http) {
  $http.get('js/data.json').success(function(data) {
    $scope.contacts = data;
    //$scope.contacts = 'name';
  });
}]);

// Contacts Details Controller
contactManager.controller('DetailsController', ['$scope', '$routeParams', '$http',
  function($scope, $routeParams, $http) {

    $scope.contact = [{
        "name": "Stephen Radford",
        "phone": "0123456789",
        "address": "123, Some Street\nLeicester\nGH1 2SR",
        "email": "stephen@email.com",
        "website": "stephenradford.me",
        "notes": ""
      },
      {
        "name": "Alan Border",
        "phone": "154648445",
        "address": "457, Some Street\nBirmingham\nLM1 2AB",
        "email": "stephen@email.com",
        "website": "alanborder.me",
        "notes": ""
      },
      {
        "name": "Misbah ul Haq",
        "phone": "8899556744",
        "address": "458, Some Street\nFaisalabad\nFD1 2MH",
        "email": "misbah@email.com",
        "website": "misbah.me",
        "notes": ""
      }
    ];

    $scope.contactId = $routeParams.contactId;

    $scope.addNew = function() {

      $scope.contact.push($scope.newData);
      $scop.newData = null;
      $scope.added = true;
    };
  }
]);

这是我的表单代码。我从表格中获取输入的内容

<form class="form-horizontal" ng-submit="addNew()" ng-controller="DetailsController">
  <input ng-model="contact.name">
  <input ng-model="contact.phone">
  <input ng-model="contact.address">
  <button>Click here to store data</button>
</form>

1 个答案:

答案 0 :(得分:0)

您需要将对象发送到数据$images = $xml->xpath('//property/images'); foreach ($images as $image) { if (isset($image->children()[1])) { var_dump($image->children()[1]->__toString()); } } 函数。

storedata

然而,你可以随心所欲地做到这一点。

<button ng-click="storedata(obj)">Click here to store data</button>