【问题标题】:pushing a nested array into a nested array angularjs将嵌套数组推入嵌套数组
【发布时间】:2016-06-03 20:45:30
【问题描述】:

我有一个控制者为投诉创建新的指控。我可以推入嵌套(子表 T2),但不知道为什么我不能推入子表(T3)的子表。

Json 中的模型如下所示:

{
"c_ID": 1,
"received_DT": "2018-01-22T00:00:00",
"aIO": [{
    "a_ID": 1,
    "c_ID": 9,
    "allegs": [{
        "alleG_ID": 33,
        "Allegation": "Failure..*",
        "disc": []
    }]
}]

}

我的添加角度范围如下所示,但我不确定我应该如何推送结果。

$scope.addAlleg = function () {
        var AIOID = this.a.aiO_ID

        $scope.errorMessage = "";
        var baseURL = "http://localhost:8000/";
        $http.post(baseURL + "api/aio/" + AIOID + "/allegs", $scope.newAlleg)
        .then(function (alleg) {
            $scope.c.aIO[0].allegs.push(alleg);
            $scope.newAlleg = {};
        }, function (error) {
            $scope.errorMessage = "Failed to save new trip" + error;
        })
        .finally(function () {
            $scope.isBusy = false;
        })
    };

当我将链接复制到邮递员时,它会发布并创建一个指控,但我知道我的成功/对结果做某事是错误的。

控制器

[HttpPost("aio/{aioid}/allegs")]
    public JsonResult Post(int aioid, [FromBody]AllegationsViewModel vm)
    {
        try
        {
            if (ModelState.IsValid)
            {
                //Map to entity

                var newAllegations = Mapper.Map<ALLEGATIONS>(vm);

                //Save to Database
                _repository.AddAllegations(aioid, newAllegations);

                if (_repository.SaveAll())
                {
                    Response.StatusCode = (int)HttpStatusCode.Created;
                    return Json(Mapper.Map<AllegationsViewModel>(newAllegations));
                }
            }
        }
        catch (Exception ex)
        {
            _logger.LogError("Failed to save new allegation", ex);
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return Json("Failed to save new allegation");
        }

        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(" Validation Failed on new checklist");
    }

我试过了

$scope.c.aIO[0].allegs.push(alleg);
$scope.c.aIO[0].allegs[0].push(alleg);
$scope.c.aIO.allegs.push(alleg);

但不断收到错误的请求。我添加到 T2(第一个子表)的范围就像我一样工作得很好

$scope.c.aIO.push(response.data);

另外,我的 html 标记如下所示:

 <input type="text" ng-model="newAlleg.Allegation"/>

【问题讨论】:

    标签: angularjs http http-post


    【解决方案1】:

    你试过 $scope.complaints.aIO[0].allegs.push(alleg.data) 吗?

    我假设 $scope.c 是 $scope.complaints

    【讨论】:

    • 我将代码修复为 $scope.c,对此感到抱歉。我试过了,还是不想发帖。
    • 你能在这里粘贴 API 响应吗?看起来像它的 API 问题而不是任何东西。
    • 唐,如果它在邮递员上工作正常,它可能是我的控制器吗?我无法在此处粘贴我的控制器,所以我用它编辑了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2022-06-14
    • 2021-02-10
    • 2023-01-12
    相关资源
    最近更新 更多