Angularjs输入表格作为json对象的数组

时间:2015-04-08 18:53:58

标签: angularjs

在html中,我想创建一个输入表单,它在帖子上给我这个结果,在注册时,我想发布用户地址作为地址数组有像 “性别”:“男性”,     “地址”:[{         “街”:“123街”,         “post_code”:“45663”     }]

Html我写的像

<input type="text" ng-model="user.addresses[].street" >
<input type="text" ng-model="user.addresses[].post_code" >.

1 个答案:

答案 0 :(得分:1)

请参阅下面的演示

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

app.controller('firstCtrl', function($scope) {
  $scope.user = {

    gender: "MALE",
    addresses: [

      {
        street: "123 street",
        post_code: "45687"
      }

    ]

  }
});
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app">
  <div ng-controller="firstCtrl" class="container">
    <label>Gender</label>
    <input type="text" ng-model="user.gender" class="form-control">
    <br/>
    <div ng-repeat="address in user.addresses">
      <label>Street</label>
      <input type="text" ng-model="address.street" class="form-control">
      <br/>
      <label>Postcode</label>
      <input type="text" ng-model="address.post_code" class="form-control">
      <br/>
    </div>
    <pre>{{user | json}}</pre>


  </div>
</body>