Javascript 如何检查http.get中的数据是否为空,并在angularjs中执行操作?

Javascript 如何检查http.get中的数据是否为空,并在angularjs中执行操作?,javascript,jquery,angularjs,angularjs-controller,angularjs-http,Javascript,Jquery,Angularjs,Angularjs Controller,Angularjs Http,我的代码: <script> function customersController($scope, $http) { $http.get("https://tic.com/testingservice/HttpService.svc/operator/Var1/Var2") .success(function (response) { $scope.names = response; }); } </script>

我的代码:

<script>  
 function  customersController($scope, $http) {
        $http.get("https://tic.com/testingservice/HttpService.svc/operator/Var1/Var2")
        .success(function (response) { $scope.names = response; });
    }
 </script>

<table class="plansbox" cellspacing="0" rules="all" id="ctl00_ContentPlaceHolder1_FULLTALKTIME" style="border-width:0px;width:100%;border-collapse:collapse;">
        <tbody><tr class="rechargeplansheader">
            <th scope="col">AMOUNT</th><th scope="col">TALKTIME</th><th scope="col">VALIDITY</th><th scope="col">DESCRIPTION</th>
        </tr><tr class="GridRow" ng-repeat="x in names | filter: { plantype: 'TopUp' }" onclick="label1('220');" style="font-family:Verdana;cursor: pointer; cursor: hand;">
            <td style="width:10%;">

                <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lblAmount" style="font-weight:bold;">{{ x.Amount }}</span>
                </td><td style="width:10%;">
                <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lbltalktime" style="font-weight:bold;">{{x.Talktime }}</span>
                </td><td style="width:10%;">
                <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lblvalidity" style="font-weight:bold;">{{ x.Validity }}</span>
                </td><td style="width:70%;">
                <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lbldescription">{{ x.Description}}</span>
                </td>
        </tr>
    </tbody></table>

函数customersController($scope,$http){
$http.get(“https://tic.com/testingservice/HttpService.svc/operator/Var1/Var2")
.success(函数(响应){$scope.names=response;});
}
AMOUNTTALKTIMEVALIDITYDESCRIPTION
{{x.金额}
{{x.Talktime}}
{{x.有效性}
{{x.Description}}
正如您所看到的,我能够获取数据,检查返回的数据是否为null,但在null的情况下要显示另一个div


请帮助我,我是angularjs的新手。

您可以简单地使用
ng if=“!names”
,为了更好地维护一个标志,该标志将为您提供有关数据获取ajax完成与否的信息。&当未找到数据时,这可能非常有用

控制器

var customersController($scope, $http) {
    $scope.dataLoaded = false; //data loading done or not
    $http.get("https://tic.com/testingservice/HttpService.svc/operator/Var1/Var2")
    .success(function(response) {
      $scope.dataLoaded = true;
      $scope.names = response;
    });
}

app.controller('customersController', ['$scope', '$http', customersController]);
标记

  <table class="plansbox" cellspacing="0" rules="all" id="ctl00_ContentPlaceHolder1_FULLTALKTIME" style="border-width:0px;width:100%;border-collapse:collapse;">
    <thead>
      <tr class="rechargeplansheader">
        <th scope="col">AMOUNT</th>
        <th scope="col">TALKTIME</th>
        <th scope="col">VALIDITY</th>
        <th scope="col">DESCRIPTION</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-if="dataLoaded && (!names || names.length === 0)" colspan="4">
        <td>No data found</td>
      </tr>
      <tr class="data-loading" ng-if="!dataLoaded" colspan="4">
        <td>Data is loading</td>
      </tr>
      <tr ng-if="dataLoaded" class="GridRow" ng-repeat="x in names | filter: { plantype: 'TopUp' }" onclick="label1('220');" style="font-family:Verdana;cursor: pointer; cursor: hand;">
        <td style="width:10%;">
          <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lblAmount" style="font-weight:bold;">{{ x.Amount }}</span>
        </td>
        <td style="width:10%;">
          <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lbltalktime" style="font-weight:bold;">{{x.Talktime }}</span>
        </td>
        <td style="width:10%;">
          <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lblvalidity" style="font-weight:bold;">{{ x.Validity }}</span>
        </td>
        <td style="width:70%;">
          <span id="ctl00_ContentPlaceHolder1_FULLTALKTIME_ctl02_lbldescription">{{ x.Description}}</span>
        </td>
      </tr>
    </tbody>
  </table>

数量
通话时间
有效性
描述
没有找到任何数据
正在加载数据
{{x.金额}
{{x.Talktime}}
{{x.有效性}
{{x.Description}}

更新

此外,您还可以添加一个标志,该标志将为您提供有关ajax的信息,此外,您还可以使用
dataLoaded
标志。当ajax正在进行时,将
dataLoaded
值设置为false,在ajax完成后将
dataLoaded
设置为true

您可以使用
ng show
ng hide
ng if
指令在html上显示或隐藏任何
div
。只需在其中指定表达式值,取决于表达式值的真/假,它将显示/隐藏
div
元素


在上面的例子中,我使用了
ng if=“dataLoaded&(!names | | names.length==0)”
,它对表达式进行求值,并将显示
div
仅当ajax完成时&names数组确实有值或其长度为零(0)

如果($scope.dataLoaded=true){alert(“data loaded”)},我将使用它每次它进入时,要么数据为空要么不为空。@user3630591您是否尝试了我在回答中给出的内容?我想在数据加载时成功显示此div。如果数据为null,我应该显示另一个div No data found,它是可见的,尽管dataloaded perfectly如果服务返回的数据为null,则执行
No data found