我是 JSON 数据新手,所以请帮助我! 我有一个带有多个表单的网页,这些表单使用 angularjs 进行验证, 我想为该表单中的字段生成 JSON 数据。那么应该使用哪些方法来生成 JSON 数据以及该方法的 post 语法。
我的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 }]
};
});
最佳答案
Angular 允许您将 ng-model
字段绑定(bind)为对象的一部分。因此,不要将每个表单字段绑定(bind)到单独的值。使它们各自成为单个对象中的属性。
<input type="text" ng-model="myForm.productName"/>
<input type="text" ng-model="myForm.productCount"/>
然后,您将拥有一个 JSON 对象 myForm
,其中包含表单中的所有值,并且可以从 Controller
进行访问。
关于html - 如何在 angularjs 中为表单字段生成 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493664/