繁体   English   中英

如果为空,我该如何写一个条件http.get来创建记录

[英]How do I write a conditional http.get to create a record if it's null angularjs

我很好奇如何在http.get服务调用中添加条件语句。

例如我的服务电话:

$http.get(baseURL + "/api/complaints/" + $scope.Case + "/clists")
    .then(function (cl) {
        //success
        $scope.cl = []
        $scope.cl = cl;

    }, function (err) {
        //failure
        var errorMessage = "Cannot post checklists" + err;
    })
    .finally(function () {
        var isBusy = false;
    });

我想添加一个条件来说明$ scope是否不存在这些clists.Case,请运行post service调用创建一个clist。 看起来像这样:

$http.get(baseURL + "/api/complaints/" + $scope.Case + "/clists"")
    .then(function (cl) {
        //success
        $scope.cl = []
        if ($scope.cl.ID == null) {
            $http.post(baseURL + "/api/complaints/" + $scope.Case + "/clists")
            $scope.cl = cl;
        }
        else {
            $scope.cl = cl;
        }
    }, function (err) {
        //failure
        var errorMessage = "Cannot post checklists" + err;
    })
    .finally(function () {
        var isBusy = false;
    });

看起来不错,除了这部分。 您需要为帖子的promise添加一个回调函数:

if ($scope.cl.ID == null) {
    $http.post(baseURL + "/api/complaints/" + $scope.Case + "/clists")
        .then(function(cl) { 
            $scope.cl = cl;
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM