2015-05-10 81 views
1

我這裏有一個plunker - http://plnkr.co/edit/AmoiJi8k000OKA6TNdcz?p=previewAngularjs NG-重複過濾器基於JSON值

我在這裏使用虛擬JSON - http://myjson.com/4aqlh

我想顯示基於頁面的不同部分的內容JSON在json上的值

所以具有Section:「New」值的故事將顯示在一個div中,而在另一個div中顯示爲「Old」。

這適用於顯示'新'的第一個div,但在只顯示'舊'的第二個div中顯示所有的故事。

過濾器有什麼問題,或者是與Angular構建頁面有關的事情。

 <div class="container" data-ng-controller="myCtrl"> 

      <div class="row"> 

       <div class="col-sm-6"> 
        <div class="story" data-ng-repeat="story in stories | filter: story.Section='New' " > 
         {{story.Title}} 
         {{story.Section}} 
        </div> 
       </div> 

       <div class="col-sm-6"> 
        <div class="story" data-ng-repeat="story in stories | filter: story.Section='Old' "> 
         {{story.Title}} 
         {{story.Section}} 
        </div> 
       </div> 

      </div> 

     </div> 
+0

如果你不能找到一個解決方案,您可以隨時添加一個'NG-if'過濾所需的對象。 – ShellFish

+0

嗯......這似乎很奇怪 –

回答

6

這是你如何與filter

  <div class="col-sm-6"> 
       <div class="story" data-ng-repeat="story in stories | filter: {Section:'New'}" > 
        {{story.Title}} 
        {{story.Section}} 
       </div> 
      </div> 

      <div class="col-sm-6"> 
       <div class="story" data-ng-repeat="story in stories | filter: {Section:'Old'}"> 
        {{story.Title}} 
        {{story.Section}} 
       </div> 
      </div> 

它需要一個對象語法過濾。 =是一個賦值操作符。請參見filter documentation

1

明確指定過濾器中的表達式,它將起作用。

工作plunker:http://plnkr.co/edit/yLIO14rlWdCFKtmg3b3N?p=preview

<div class="container" data-ng-controller="myCtrl"> 

    <div class="row"> 

     <div class="col-sm-6"> 
      <div class="story" data-ng-repeat="story in stories | filter: story: story.Section='New' " > 
       {{story.Title}} 
       {{story.Section}} 
      </div> 
     </div> 

     <div class="col-sm-6"> 
      <div class="story" data-ng-repeat="story in stories | filter: story: story.Section='Old' "> 
       {{story.Title}} 
       {{story.Section}} 
      </div> 
     </div> 

    </div> 

</div>