javascript
  • angularjs
  • 2014-04-10 166 views 0 likes 
    0

    這裏不工作是的jsfiddle,我可以得到它與真,假值工作,但是當我嘗試通過名稱來過濾,它不工作有什麼建議?jsfidle濾波器angularjs

    jsFiddle

    <div ng-controller="MyCtrl"> 
    
        <button class="btn" ng-click='filterCriteria={}'>All</button> 
        <button class="btn" ng-click='filterCriteria.read=true'>Read</button> 
        <button class="btn" ng-click='filterCriteria.title={{messages[0].title}}'>Foo</button> 
        <pre>{{messages[0].title}}</pre> 
    
        <hr/> 
        <table class="table table-bordered"> 
         <thead> 
          <tr> 
           <td><strong>Title</strong></td> 
           <td><strong>Content</strong></td>  
           <td><strong>Read</strong></td>  
          </tr> 
         </thead> 
         <tbody> 
          <tr ng-repeat='message in messages |filter:filterCriteria'> 
           <td>{{message.title}}</td> 
           <td>{{message.content}}</td>  
           <td>{{message.read}}</td>  
          </tr> 
         </tbody>    
        </table> 
    </div> 
    
    
        function MyCtrl($scope) { 
    
        $scope.filterCriteria = {}; 
    
        $scope.messages = [{ 
         title: 'Foo', 
         content: 'Foo content', 
         read: false}, 
        { 
         title: 'Bar', 
         content: 'Bar content', 
         read: true} 
          ]; 
        } 
    
    +0

    例如'$ scope.filterCriteria = {title:'Foo'};' - 只顯示foo行,您的問題是什麼? – przno

    回答

    0

    我覺得你不需要使用{{}}在NG-單擊

    使用()

    嘗試

    <button class="btn" ng-click='filterCriteria.title=(messages[0].title)'>Foo</button> 
    

    http://jsfiddle.net/JtAZM/43/

    3

    由於您的filterCriteria不被清除,你所面臨的問題。

    Fiddle

    嘗試下面的過濾的名字:

    $scope.filterByName= function(name){ 
        $scope.filterCriteria={}, 
        $scope.filterCriteria.title=name; 
    } 
    

    注:我假設你是問有關新的過濾被點擊按鈕老過濾器去除。

    +0

    這比我的答案更好! – mainguy

    +0

    這是一個很好的答案,所有的邏輯都應該移到控制器上。 – UberChris

    0

    剛剛離開了括號:

    <button class="btn" ng-click='filterCriteria.title=messages[0].title'>Foo</button> 
    

    工程here

    +0

    @UberChris給出了基本相同的答案。 – mainguy

    相關問題