基于json值的Angularjs ng-repeat过滤器

时间:2015-05-10 14:07:30

标签: angularjs angularjs-ng-repeat angularjs-filter

我这里有一个傻瓜 - http://plnkr.co/edit/AmoiJi8k000OKA6TNdcz?p=preview

我在这里使用虚拟json - http://myjson.com/4aqlh

我想根据json

中的值在页面的不同部分显示json内容

所以故事的值为Section:" New"将显示在一个div和' Old'在另一个。

这适用于第一个div' New'虽然是显而易见的,但在第二个div中,它只显示“Old”,它会显示所有的故事。

过滤器是否有问题,或者与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>

2 个答案:

答案 0 :(得分:6)

这是您使用stdClass Object ( [labels] => stdClass Object ( [name] => Categories [singular_name] => Category [search_items] => Search Categories [popular_items] => [all_items] => All Categories [parent_item] => Parent Category [parent_item_colon] => Parent Category: [edit_item] => Edit Category [view_item] => View Category [update_item] => Update Category [add_new_item] => Add New Category [new_item_name] => New Category Name [separate_items_with_commas] => [add_or_remove_items] => [choose_from_most_used] => [not_found] => No categories found. [menu_name] => Categories [name_admin_bar] => category ) [description] => [public] => 1 [hierarchical] => 1 [show_ui] => 1 [show_in_menu] => 1 [show_in_nav_menus] => 1 [show_tagcloud] => 1 [show_in_quick_edit] => 1 [show_admin_column] => 1 [meta_box_cb] => post_categories_meta_box [rewrite] => Array ( [hierarchical] => 1 [slug] => category [with_front] => 1 [ep_mask] => 512 ) [query_var] => category_name [update_count_callback] => [_builtin] => 1 [cap] => stdClass Object ( [manage_terms] => manage_categories [edit_terms] => manage_categories [delete_terms] => manage_categories [assign_terms] => edit_posts ) [name] => category [object_type] => Array ( [0] => post ) [label] => Categories )

进行过滤的方法
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 :(得分:1)

明确指定过滤器中的表达式,它将起作用。

工作人员: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>