Невозможно отобразить «Не найдено» с помощью ng-hide и фильтров в Angular JS

0

Я пытаюсь реализовать мгновенный поиск с использованием фильтров в угловых js. Я хочу "Не найдено!" сообщение, которое будет отображаться, когда фильтр пуст, т.е. когда не найдено совпадающего результата. Директива ng-hide не работает, когда фильтр-массив пуст.

<!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>
Показать ещё 1 комментарий
Теги:
search

2 ответа

0
Лучший ответ

Перемещение ng-hide вне ng-repeat работало !!

<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>
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>
  • 0
    Вы добавили ng-hide внутри цикла, поэтому он не отображается.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню