带有json文件和嵌套数组Angularjs的http post

http post with json file and nested array Angularjs

本文关键字:Angularjs http post 数组 嵌套 json 文件 带有      更新时间:2023-09-26

我有一个应用程序,它能够通过关系数据库模型通过http获取和发布。我能够显示我的数据,JSON 数据在 http://localhost/**/api/complaint 上如下所示:

{
"CHECKLISTs": [{
    "COMP_ID": 1,
    "IntOIMRec": "No"
}],
"COMP_ID": 1,
"FileNum": "case1"
}

使用角度:

<tr data-ng-repeat="complaint in complaints">
<td><strong data-ng-hide="complaint.editMode">{{ complaint.FileNum }}</strong></td>
<td>
<p data-ng-hide="complaint.editMode">{{ complaint.CHECKLISTs[0].IntIAB}}</p>
<input data-ng-show="complaint.editMode" type="text" data-ng-model="customer.Status" />
</tr>

我还可以使用此控制器进行http发布:

//Insert complaint
$scope.add = function () {
    $scope.loading = true;
    $http.post('/api/Complaint/', this.newcomplaint).success(function (data) {
        alert("Added Successfully!!");
        $scope.addMode = false;
        $scope.complaints.push(data);
        $scope.loading = false;
    }).error(function (data) {
        $scope.error = "An Error has occured while Adding complaint! " + data;
        $scope.loading = false;
    });
};

但是当我将清单[0]添加到我的表单中添加它时,它不会添加新记录。我用于添加新记录的表单代码如下所示:

<div class="row">
    <div class="col-md-12">
        <strong class="error">{{ error }}</strong>
        <p data-ng-hide="addMode"><a data-ng-click="toggleAdd()" href="javascript:;" class="btn btn-primary">Add New</a></p>
        <form name="addComplant" data-ng-show="addMode" style="width:600px;margin:0px auto;">
            <div class="form-group">
                <label for="cid" class="col-sm-2 control-label">ID:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="cid" placeholder="please enter id" data-ng-model="newcomplaint.FileNum" required />
                </div>
            </div>
            <div class="form-group">
                <label for="cname" class="col-sm-2 control-label">Name:</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="cname" placeholder="please enter your name" data-ng-model="newcomplaint.CHECKLISTs[0].IntIAB" />
                </div>
            </div>
            <br />
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="submit" value="Add" data-ng-click="add()" class="btn btn-primary" />
                    <input type="button" value="Cancel" data-ng-click="toggleAdd()" class="btn btn-primary" />
                </div>
            </div>
            <br />
        </form>
    </div>
</div>

我的控制台.log输出:

控制台.log

有人可以告诉我,如果我可以使用角度 http.get 和 http.post 来显示它,为什么当它清楚地在 JSON 文件中并且显示正常时,我不能添加到清单字段中?我得到的错误是 [对象对象] ?

谢谢

仅通过查看您的代码,您似乎正在推送$scope.complaints,而不是$scope.complaint.CHECKLISTSs

你在某处有ng重复吗?

$scope.complaints.push(data);更改为$scope.newcomplaint = data。或者,如果投诉真的应该是一个数组,那么您需要在表单上ng-repeat