在JSON中返回表单并将其作为嵌套元素放在Angular JS中

时间:2015-08-10 01:28:46

标签: javascript json angularjs

我有一个HTML表单,它返回一个JSON对象,但我希望这些数据是嵌套的JSON。

这是我的HTML

    <form role="form" ng-submit="submitWorkshop()">
   <div>
     <div ng-repeat="x in form">
       <h4>{{x.field_label}}</h4>
          <div ng-switch on="x.field_ui_type">
             <textarea class="form-control" ng-switch-when="textarea" placeholder="{{x.field_name}}" ng-model="result.Table_Name[x.field_name]"></textarea>
            <input class="form-control" ng-switch-when="input" placeholder="{{x.field_name}}" ng-model="result.Table_Name[x.field_name]">

          </div>

      </div>

    </div>

    <hr>

    <div align="right">
       <button  type="submit" class="btn btn-default" ng-click="submitWorkshop()">Submit</button>
       <button type="reset" class="btn btn-default">Reset</button>
                        </div>
   </form>

这是我的角度控制器

app.controller('createworkshopController', function($scope, $http) {

$http.get(end_url + ':' + port + '/createworkshop')
    .success(function(data) {
        $scope.form = data.Workshop;
        $scope.Table_Name = data.Workshop[1].table_name;
        console.log($scope.Table_Name);
    });

$scope.result = {Table_Name: {}};
$scope.submitWorkshop = function() {

    console.log($scope.result);

  // $http({method:'POST', data:$scope.result, url:'/workshopreturn'})
};

});

我制作表单的JSON数据(部分)

Workshop: [
{
  md_id: 35,
  form: "createworkshop",
  table_name: "d_workshop",
  field_name: "WorkshopId",
  visibility: 0,
  field_label: "Workshop ID",
  field_ui_type: "input",
  field_type: "int",
  display_order: 1,
  field_value: 0
  },...........

目前我可以在table_name中获取d_workshop,但是当我尝试将table_name存储在$ scope.Table_Name中时,我尝试将数据嵌套到后端。

现在每当我从html获得JSON时我都会得到这个

Table_Name: {
   Age: "a",
   Agenda: "a",
   Concept: "a",
   CurriculumId: "a",
   Objectives: "a",
   Subject: "a",
   TargetNoOfStudents: "a",
   Title: "a",
   WorkshopId: "a",
}

但我希望它是动态的,在这种情况下应该是

d_workshop: {
   Age: "a",
   Agenda: "a",
   Concept: "a",
   CurriculumId: "a",
   Objectives: "a",
   Subject: "a",
   TargetNoOfStudents: "a",
   Title: "a",
   WorkshopId: "a",
}

请帮助

0 个答案:

没有答案