我很想知道如何在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不存在clists,请运行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;
});
答案 0 :(得分:1)
除了这部分外,它看起来很好。您需要为帖子的承诺添加回调函数:
if ($scope.cl.ID == null) {
$http.post(baseURL + "/api/complaints/" + $scope.Case + "/clists")
.then(function(cl) {
$scope.cl = cl;
}
}