AngularJS-필터의 빈 결과에 대한 자리 표시 자 … HTML : <div ng-controller=”Ctrl”> <h1>My Foo</h1> <ul>

예를 들어 <No result>필터 결과가 비어있을 때 자리 표시자를 갖고 싶습니다 . 누구든지 도와 주시겠습니까? 어디서부터 시작해야할지 모르겠어요 …

HTML :

<div ng-controller="Ctrl">
<h1>My Foo</h1>
<ul>
    <li ng-repeat="foo in foos">
        <a href="#" ng-click="setBarFilter(foo.name)">{{foo.name}}</a>
    </li>
</ul>
<br />
<h1>My Bar</h1>
<ul>
    <li ng-repeat="bar in bars | filter:barFilter">{{bar.name}}</li>
</ul>

</div>

JS :

function Ctrl($scope) {

  $scope.foos = [{
    name: 'Foo 1'
  },{
    name: 'Foo 2'
  },{
    name: 'Foo 3'
  }];

  $scope.bars = [{
    name: 'Bar 1',
    foo: 'Foo 1'
  },{
    name: 'Bar 2',
    foo: 'Foo 2'
  }];

  $scope.setBarFilter = function(foo_name) {
    $scope.barFilter = {};
    $scope.barFilter.foo = foo_name;
  }
}

jsFiddle : http://jsfiddle.net/adrn/PEumV/1/

감사!



답변

필터를 한 번만 지정하면되는 접근 방식에 대한 조정 :

  <li ng-repeat="bar in filteredBars = (bars | filter:barFilter)">{{bar.name}}</li>
</ul>
<p ng-hide="filteredBars.length">Nothing here!</p>

깡깡이


답변

다음은 ng-show를 사용하는 트릭입니다.

HTML :

<div ng-controller="Ctrl">
<h1>My Foo</h1>
<ul>
    <li ng-repeat="foo in foos">
        <a href="#" ng-click="setBarFilter(foo.name)">{{foo.name}}</a>
    </li>
</ul>
<br />
<h1>My Bar</h1>
<ul>
    <li ng-repeat="bar in bars | filter:barFilter">{{bar.name}}</li>
</ul>
<p ng-show="(bars | filter:barFilter).length == 0">Nothing here!</p>

</div>

jsFiddle : http://jsfiddle.net/adrn/PEumV/2/


답변

공식 문서 에서 가져온 것입니다.

ng-repeat="friend in friends | filter:q as results"

그런 다음 결과를 배열로 사용

<li class="animate-repeat" ng-if="results.length == 0">
  <strong>No results found...</strong>
</li>

전체 스 니펫 :

<div ng-controller="repeatController">
I have {{friends.length}} friends. They are:
<input type="search" ng-model="q" placeholder="filter friends..." aria-label="filter friends" />


<ul class="example-animate-container">
    <li class="animate-repeat" ng-repeat="friend in friends | filter:q as results">
      [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
    </li>
    <li class="animate-repeat" ng-if="results.length == 0">
      <strong>No results found...</strong>
    </li>
  </ul>
</div>


답변