2015-02-23 106 views
0

这是一个带有angularjs的表单。AngularJS提交json

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
 
</head> 
 

 
<body> 
 
<div ng-app="" ng-controller="formController"> 
 
    <form novalidate> 
 
    First Name:<br> 
 
    <input type="text" ng-model="user.firstName"><br> 
 
    Last Name:<br> 
 
    <input type="text" ng-model="user.lastName"> 
 
    <br><br> 
 
    <button ng-click="reset()">RESET</button> 
 
    </form> 
 
    <p>form = {{user }}</p> 
 
</div> 
 

 
<script> 
 
function formController ($scope) { 
 
    $scope.master = {firstName:"John", lastName:"Doe"}; 
 
    $scope.reset = function() { 
 
     $scope.user = angular.copy($scope.master); 
 
    }; 
 
    $scope.reset(); 
 
} 
 
</script> 
 

 
</body>

我不熟悉AngularJS,我发现了表达{{用户}}显示为像格式的数据的JSON将其绑定到文本框。我想知道是否有任何方法可以将{{user}}作为json直接发布到服务器端。我知道我可以轻松地发布表单。但我的目的是页面可能有很多文本框。我想使用json存储它,就像{{user}}一样,并将其作为一个组合提交。 而且我也想知道如何删除表达式中的一个值。即以下代码具有两个属性firstName和lastName。在调用它之后,有没有类似remove()的方法?我可以删除相应json数据中的一个属性。 非常感谢

回答

0

您可以使用$ http直接将您的模式(本例中为用户)作为JSON发布。

如果你的文本字段都保存在用户模态则U可以通过调用这个函数的去除的属性之一提交表单

$scope.remove = function (user) { 
    delete $scope.user.firstName;//the output here is user with only lastname 
    }