如何显示在ng repeat for filter中找不到匹配项的错误消息

时间:2019-01-17 18:45:35

标签: javascript html angularjs

我有一个文本框和几个来自ng repeat的div,当我在文本框上搜索任何内容时,它将显示基于搜索的结果。但是如果找不到匹配项,我应该得到一条错误消息,我在这里使用了ng show基于值,但该消息未显示。有人可以帮我吗。我是angular js的新手,这是下面的代码

html

<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js'></script>
<script  src="script.js"></script>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.css'>
 <body ng-app="app">     
  <div ng-controller="AccordionDemoCtrl">
    <div class="scroll-div"> 
<div><input type="text" placeholder="search" ng-model="item"/>  
      <div style="border:1px solid;" id="anchor{{group.id}}"  ng-repeat="group in groups | filter:item">

          {{ group.title }}   
        </div> 
      <p ng-show="!groups.length">No record found</p>       
      </div>
    </div>

  </div>
  </body>

script.js

var app=angular.module('app', ['ui.bootstrap','ngSanitize','angular.filter']);
app.controller('AccordionDemoCtrl', function ($scope,$location,$anchorScroll) {
  $scope.oneAtATime = true;

  $scope.groups = [
    {
      title: 'title 1',
      id:'1',
      list: ['<i>item1a</i> blah blah',
        'item2a'       
        ]
    },
    {
      title: 'title 2',
      id:'2',
      list: ['<i>item1a</i> blah blah',        
        'item3a'        
        ]
    },
    {
      title: 'title 3',
      id:'3',
      list: ['<i>item1a</i> blah blah',       
        'item4a'        
        ]
    },
    {
      title: 'title 4',
      id:'4',
      list: ['<i>item1a</i> blah blah',      
        'item5a'        
        ]

    },
    {
      title: 'title 5',
      id:'6',
      list: ['<i>item1a</i> blah blah',       
        'item6a'        
        ]
    },
    {
      title: 'title 6',
      id:'5',
      list: ['<i>item1a</i> blah blah',        
        'item7a'
        ]
    }
  ];
  $scope.test = function() {    
  $scope.predicate = 'id'; 
  $scope.reverse=true;
  }
$scope.groups[0].isOpen = true;
});

1 个答案:

答案 0 :(得分:1)

尝试一下:

<div ng-show="!(groups | filter:item).length">No record found</div> 

示例:http://plnkr.co/edit/HCVcneFSOhyN3IWQbX75?p=preview