Angular 在 ng-repeat 中搜索不工作

Angular search within ng-repeat not working

所以我有一个针对某些出版物的小过滤器,总的来说它工作正常,但是,我遇到了一个问题,即子术语过滤器只有在我之前点击它的父项时才起作用,这是我的猜测这是一个范围问题,但我不知道如何解决它,非常感谢任何帮助。

编辑:使用 Angular 过滤器,我已经忘记 post 完整的片段

<div class="row">
    <div class="onefourth">
        <div class="linklist terms">
            <ul>
                <li ng-repeat="term in terms">
                    <div ng-class="{parent: term.parent == null, active: $first }" ng-show="term.parent==null">
                        <input type="radio" name="terms" id="label_{{$index}}" ng-value="{{term.ID}}" ng-model="$parent.search.terms.categoria_pub.ID">
                        <label for="label_{{$index}}">{{term.name}}</label> 

                    </div>
                    <ul>
                        <li ng-repeat="childTerm in terms">
                            <div ng-if="childTerm.parent.ID==term.ID">
                                <input type="radio" name="terms" id="label_{{$index}}" ng-value="{{childTerm.ID}}" ng-model="$parent.search.terms.categoria_pub.ID">
                                <label for="label_{{$index}}">{{childTerm.name}}</label> 

                            </div>
                        </li>
                    </ul>
                </li>

            </ul>
        </div>
    </div>
    <div class="threefourths">
        <div class="row">
            <div ng-repeat="file in files | filter:search:strict">
                <div class="publication">
                    <div class="onefourth">
                        <a ng-href="{{ file.custom_fields.jcf_link[0] }}" target="_blank">
                            <div class="imgholder">
                                <img ng-src="{{ file.featured_image.source }}" src="<?php echo get_template_directory_uri(); ?>/img/placeholder_pub.jpg">
                            </div>
                        </a>
                    </div>
                    <div class="threefourths">
                        <h4>{{file.title}}</h4>
                        <div class="content" ng-bind-html="file.content | safeHtml">

                        </div>
                        <br>
                        <a ng-href="{{ file.custom_fields.jcf_link[0] }}" class="title" target="_blank">Descargar Publicación</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

编辑:一些示例数据

[
    {
        "ID": 19,
        "name": "Libros, Monografías y notas técnicas",
        "slug": "libros-monografias-notas",
        "description": "",
        "taxonomy": "categoria_pub",
        "parent": {
            "ID": 4,
            "name": "Documentos ",
            "slug": "documentos-",
            "description": "",
            "taxonomy": "categoria_pub",
            "parent": null,
            "count": 9,
            "link": "http://www.example.com/categoria_pub/documentos-/",
            "meta": {
                "links": {
                    "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                    "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
                }
            }
        },
        "count": 5,
        "link": "http://www.example.com/categoria_pub/libros-monografias-notas/",
        "meta": {
            "links": {
                "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/19"
            }
        },
        "$$hashKey": "object:23"
    },
    {
        "ID": 30,
        "name": "Todo",
        "slug": "todo",
        "description": "",
        "taxonomy": "categoria_pub",
        "parent": null,
        "count": 10,
        "link": "http://www.example.com/categoria_pub/todo/",
        "meta": {
            "links": {
                "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/30"
            }
        },
        "$$hashKey": "object:24"
    },
    {
        "ID": 4,
        "name": "Documentos ",
        "slug": "documentos-",
        "description": "",
        "taxonomy": "categoria_pub",
        "parent": null,
        "count": 9,
        "link": "http://www.example.com/categoria_pub/documentos-/",
        "meta": {
            "links": {
                "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
            }
        },
        "$$hashKey": "object:25"
    },
    {
        "ID": 17,
        "name": "Breves, Informes de opinión",
        "slug": "breves",
        "description": "",
        "taxonomy": "categoria_pub",
        "parent": {
            "ID": 4,
            "name": "Documentos ",
            "slug": "documentos-",
            "description": "",
            "taxonomy": "categoria_pub",
            "parent": null,
            "count": 9,
            "link": "http://www.example.com/categoria_pub/documentos-/",
            "meta": {
                "links": {
                    "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                    "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
                }
            }
        },
        "count": 5,
        "link": "http://www.example.com/categoria_pub/breves/",
        "meta": {
            "links": {
                "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/17"
            }
        },
        "$$hashKey": "object:26"
    },
    {
        "ID": 20,
        "name": "Artículos recomendados",
        "slug": "articulos-recomendados",
        "description": "",
        "taxonomy": "categoria_pub",
        "parent": null,
        "count": 0,
        "link": "http://www.example.com/categoria_pub/articulos-recomendados/",
        "meta": {
            "links": {
                "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/20"
            }
        },
        "$$hashKey": "object:27"
    },
    {
        "ID": 15,
        "name": "Transcripciones webinars",
        "slug": "transcripciones-webinars",
        "description": "",
        "taxonomy": "categoria_pub",
        "parent": {
            "ID": 4,
            "name": "Documentos ",
            "slug": "documentos-",
            "description": "",
            "taxonomy": "categoria_pub",
            "parent": null,
            "count": 9,
            "link": "http://www.example.com/categoria_pub/documentos-/",
            "meta": {
                "links": {
                    "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                    "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
                }
            }
        },
        "count": 0,
        "link": "http://www.example.com/categoria_pub/transcripciones-webinars/",
        "meta": {
            "links": {
                "collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
                "self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/15"
            }
        },
        "$$hashKey": "object:28"
    }
]

编辑:jsfiddle 抱歉花了这么长时间

https://jsfiddle.net/785p43cb/22/

使用 AngularJS filters 怎么样?

https://docs.angularjs.org/api/ng/filter/filter

这是自定义过滤器的示例:

http://jsfiddle.net/hm8qD/3/

请考虑修改customFilter来解决您的需求。

我已经 fork 你的 fiddle 并更新了它。这是:

https://jsfiddle.net/ignaciovillaverde/5g6weppn/4/

它的工作方式有点不同。首先,我删除了 file in files | filter:search 中的 de strict 比较器,你没有使用它。我还将您的过滤器分为两种不同的方式,一种使用文本类型输入,另一种使用单选按钮。对于单选按钮,我使用了 ng-show="hasSelectedCategory(file.terms.categoria_pub) || !selectedCategory"

另请注意,对于单选按钮中的 ng-model 指令,我需要使用 ng-model="$parent.selectedCategory"ng-model="$parent.$parent.selectedCategory"

我还包含了一个询问文件是否具有所选类别的功能。看看吧。

希望对您有所帮助!