【问题标题】:how to generate json data for a form fields in angularjs如何为angularjs中的表单字段生成json数据
【发布时间】:2015-12-28 12:36:27
【问题描述】:

我是 JSON 数据的新手,所以请帮帮我!! 我有多个表单的单个网页,这些表单使用 angularjs 进行验证, 我想为该表单中的字段生成 JSON 数据。那么应该使用哪些方法来生成 JSON 数据并为该方法发布语法。

我的html代码是这样的

<td >
<input type="text"  style="width:100px" ng-model="cost"  ng-disabled="status!=true" required>
</td>
<td >
<input type="text"  style="width:110px" ng-model="price" ng-disabled="status!=true" required>
</td>

我的 angularjs 代码是

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

// create angular controller
validationApp.controller('mainController', function($scope) {

$scope.style
{
    $scope.buttonCSS= "clicked";
};
$scope.style2
{
}
$scope.invoice = {
    items: [{
         loyality: [{name:'points'},{name:'credits'}],
         value:0,
    period: [{name:'activity1'},{name:'activity2'}]
        }],
        items1:[{currency: [{name:'point'},{name:'credit'}],   value1:0   }]

};
 });

【问题讨论】:

    标签: html angularjs json


    【解决方案1】:

    Angular 允许您将 ng-model 字段绑定为对象的一部分。因此,不要将每个表单字段绑定到一个单独的值。使它们各自成为单个对象中的属性。

    <input type="text" ng-model="myForm.productName"/>
    
    <input type="text" ng-model="myForm.productCount"/>
    

    然后,您将拥有一个 JSON 对象 myForm,其中包含表单中的所有值,并且可以从 controller 访问。

    【讨论】:

      【解决方案2】:

      了解ng-model 以及双向绑定的工作原理。

      <input type="text" ng-model="yourobj.name"  />
      <form name="myForm" ng-click="submit()">
      

      在您的controller 中,您可以获取如下表单输入字段。

      (function(){
      
         angular.module("yourapp").controller(['$scope', function(){
      
           $scope.submit=function(){
              var jsonData = $scope.yourObj;  
              console.log(jsonData); 
           }
      
         }])
      
      })();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        相关资源
        最近更新 更多