简体   繁体   English

在Angularjs中发布json对象

[英]post json object in Angularjs

i ask this question ago but don't get answer. 我之前问过这个问题,但没有得到答案。 i have a form that by filling it create json object and post to server.this form repeat in several time. 我有一个表格,通过填充它创建json对象并发布到server.this表格重复几次。 The data entry forms can be repeated several times. 数据输入表格可以重复多次。

<div ng-repeat="office in offices">
   <input type="text" ng-model="officeName">
   <input type="text" ng-model="office.employee">
   <input type="text" ng-model="office.employee">
   <button ng-click="addOffice()">Add New Office</button>
</div>

suppose my objects are 假设我的对象是

public class FormData{
   private List<Data> all;
}
public class Data{
  private String officeName;
  private List<Employee> list;
}
public class Employee{
  private String name;
}

how create json objects and bind data that get from form bind to this objects? 如何创建json对象并绑定从表单获取的数据绑定到该对象? And how create form entry data?(how set ng-model) 以及如何创建表单条目数据?(如何设置ng-model)

You can do something like this: 您可以执行以下操作:

 var app = angular.module('app', ['ngMockE2E']); app.controller('myController', function($scope, $http) { $scope.offices = []; $scope.addOffice = function() { $scope.offices.push({ employees: [] }); }; $scope.addEmployee = function(office) { office.employees.push({}); }; $scope.submitOffices = function() { $http.post('/offices', $scope.offices) .success(function() { // Handle success. }).error(function() { // Handle error. }); }; }); 
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.3/angular-mocks.js"></script> <div ng-app='app' ng-controller='myController'> <button ng-click="addOffice()">Add New Office</button> <div ng-repeat="office in offices" ng-if="offices"> <form name="officesForm" novalidate ng-submit="submitOffices()"> Company Name: <input type="text" ng-model="office.name"> <div> Employees: <ng-form name="employeForm{{$index}}" ng-repeat="employee in office.employees"> <div> <input type="text" ng-model="employee.name"> </div> </ng-form> <button type="button" ng-click="addEmployee(office)">Add Employee</button> </div> <button type="submit">submit</button> </form> </div> <pre>{{ offices | json }}</pre> </div> 

The sandboxed iframe prevents it from posting so here it is on plunker. 装有沙盒的iframe阻止了它的发布,因此它位于插销上。

http://plnkr.co/edit/2eGVa3tg3TtIhFXNpUGI?p=preview http://plnkr.co/edit/2eGVa3tg3TtIhFXNpUGI?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM