我很好奇如何在 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.Case 不存在 clist,请运行后期服务调用来创建 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;
}
}
关于javascript - 如果它为空,如何编写条件http.get来创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946324/