使用过滤器的结果显示基于值的视图

时间:2017-07-15 17:59:43

标签: javascript angularjs

我正在尝试根据过滤器的值显示和隐藏页面的某些部分。如果数据的长度为1,则应显示某个字段,如果其等于零,则应显示该页面的不同部分

<div ng-show="results.length=0">
  <h4 class="redtext" align="center">Sorry, Search found no records </h4>
</div>
<div ng-show="results.length!=0" ng-repeat="item in record | dateRange : from : to">
  <table width="100%" border="0" cellspacing="5" cellpadding="5">
    <tr>
      <td width="70%"><div align="left">
        <p><strong>Bill Period</strong>      </p>
      </div></td>
      <td width="82%">{{item.Date |date: 'MMMM yyyy'}}</td>
    </tr>
    <tr>
      <td><p><strong>Reading Date</strong></p></td>
      <td>{{item.Date |date: 'dd MMMM yyyy'}}</td>
    </tr>
    <tr>
      <td colspan="2">&nbsp;</td>
    </tr>
  </table>

JS

$scope.state_request = function() {
 $http.post("http://localhost/server/statement.php", {
     'id': $scope.account_number
 }).success(function(data) {
     console.log(JSON.stringify(data));
     $scope.record = data;
 })
 $scope.from = $filter('date')($scope.sdate, "yyyy-MM-dd" + 'T00:00:00') + 'Z';
 $scope.to = $filter('date')($scope.edate, "yyyy-MM-dd" + 'T00:00:00') + 'Z';
 }
 })
 .filter('dateRange', function() {
     return function(records, from, to) {
         // return empty array if input not defined or not array
         if (!records || !angular.isArray(records)) {
             return [];
         }
         var results = records.filter(function(record) {
             // run console log tests here...before the return
             return record.Date >= from && record.Date <= to;
         });
         if (results.length = 0) {
             console.log('its empty')
         } else {
             console.log('results found')
         }
         console.log('Number of results:', results.length);
         return results;
     }
 })

执行脚本时没有任何内容

1 个答案:

答案 0 :(得分:0)

如果我清楚地了解你,as results之后filter(在您的情况下:ng-repeat="item in record | dateRange : from : to as results")会有所帮助:

&#13;
&#13;
angular.module('app',[]).controller('ctrl', ['$scope', function($scope){
  $scope.record = ['one', 'two', 'three', 'four'];
}]);
&#13;
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>

<div ng-app='app' ng-controller='ctrl'>
  <input type='text' ng-model='search' ng-init='search = "five"'/>
  <p ng-if='results.length == 0'>Sorry, Search found no records</p>
  <ul>
    <li ng-repeat='item in record | filter : search as results'>{{item}}</li>
  </ul>
</div>
&#13;
&#13;
&#13;

你也在条件:<div ng-show="results.length==0"></div>

上输了错字