2014-02-25 40 views
1

我有一个AgularJS搜索使用过滤器,它的工作原理。我现在希望过滤器只显示与过滤器匹配的结果。AngularJS ng-show,ng-hide

所以,我想初始数据加载不显示,如果我在过滤器中输入匹配,然后显示结果。

这是到目前为止我的代码:

<div class="row"> 
    <div class="large-12 columns"> 
    <div ng-app> 
     <div ng-controller="CashTransController">  
     <input type="text" ng-model="search.$"> 

     <br /> 

     <div><strong>Filter Results</strong></div> 

     <br /> 

     <div class="row"></div> 

     <br/> 

     <table border="0" class="items"> 
      <thead> 
      <tr> 
       <th>Purchaser</th> 
       <th>Redemption Code</th> 
       <th>Amount</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr ng-repeat="item in items | filter:search"> 
       <td>{{item.firstname}} {{item.lastname}}</td> 
       <td valign="top"><h3>{{item.redemption_code}}</h3></td> 
       <td><h3><a href="#">{{item.creation_date}}</a></h3></td>  
      </tr> 
      </tbody> 
     </table> 

     <br /> 
     </div> 
    </div> 
    </div> 
</div> 

回答

4

你可以简单地添加一个ng-show表项。

我不知道你为什么叫你的搜索模型搜索$而不只是搜索。

试试看看这个代码。它只会显示的结果,如果你输入的东西到输入:

<div class="row"> 
    <div class="large-12 columns"> 
     <input type="text" ng-model="search"> 
     <br/> 

     <div><strong>Filter Results</strong></div> 
     <br/> 

     <div class="row"> 
      <p>{{search}}</p> 
     </div> 
     <br/> 
     <table border="0" class="items"> 
      <thead> 
      <tr> 
       <th>Purchaser</th> 
       <th>Redemption Code</th> 

       <th>Amount</th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr ng-show="search" ng-repeat="item in items | filter:search"> 
       <td>{{item.firstname}} {{item.lastname}}</td> 
       <td valign="top"><h3>{{item.redemption_code}}</h3></td> 
       <td><h3><a href="#">{{item.creation_date}}</a></h3></td> 
      </tr> 
      </tbody> 
     </table> 
     <br/> 
    </div> 
</div> 
+0

作品,但搜索完成后,你删除过滤文本,整个列表中可见,这是我不想要什么。 –

+0

这就是你必须使用ng-model =“search”而不是ng-model =“search。$”的原因。那么它应该在你删除过滤文本时工作。 – DanEEStar

+0

嘿!有用。我认为搜索。$是以前版本的angularJS的补充。 –