2015-05-10 83 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>