0

I am trying to implement instant search using filters in angular js. I want the "Not Found!!" message to be displayed when the filter is empty i.e. when no matching result is found. The ng-hide directive doesn't seem to work when filter array is empty.

<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body ng-controller="places as p">
  <div>

    <input type="text" ng-model="cname" />
    <div ng-repeat="country in abc=(p.countries|filter:cname)">
      <h1>{{country.name}}</h1>{{abc.length}}
      <span ng-hide="abc.length">Not Found!!</span>

    </div>
  </div>
  <script>
    var app = angular.module('myApp', []);

    var arr = [{

      name: 'germany'
    }, {

      name: 'mexico'
    }, {

      name: 'india'
    }, {

      name: 'UK'
    }, {

      name: 'argentina'
    }];


    app.controller('places', function() {

      this.countries = arr;
    });
  </script>
</body>

</html>

3
  • Possible duplicate of AngularJS display message when object is empty in repeater Commented Oct 27, 2015 at 10:17
  • Try moving the ng-hide outside ng-repeat. Commented Oct 27, 2015 at 10:22
  • Thank You. Moving the ng-hide outside ng-repeat worked!! . :) Commented Oct 27, 2015 at 12:13

3 Answers 3

0

You should update your code like below:

    <div ng-repeat="country in abc = (p.countries | filter:cname)">
      <h1>{{country.name}}</h1>{{abc.length}}
      <span ng-show="abc.length < 1">Not Found!!</span>
    </div>

UPDATED: ng-hide changed to ng-show

Sign up to request clarification or add additional context in comments.

Comments

0

Moving the ng-hide outside ng-repeat worked!!

<input type="text" ng-model="cname" />
<div ng-repeat="country in abc=(p.countries|filter:cname)">
  <h1>{{country.name}}</h1>{{abc.length}}
</div>
<span ng-hide="abc.length">Not Found!!</span>

Comments

0

<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

<body ng-controller="places as p">
  <div>

    <input type="text" ng-model="cname" />
    <div ng-repeat="country in abc=(p.countries|filter:cname)">
      <h1>{{country.name}}</h1>{{abc.length}}
      
    </div>
    <span ng-hide="abc.length">Not Found!!</span>
  </div>
  <script>
    var app = angular.module('myApp', []);

    var arr = [{

      name: 'germany'
    }, {

      name: 'mexico'
    }, {

      name: 'india'
    }, {

      name: 'UK'
    }, {

      name: 'argentina'
    }];


    app.controller('places', function() {

      this.countries = arr;
    });
  </script>
</body>

</html>

1 Comment

You have added ng-hide inside the loop thats why it's not display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.