如何通过对象的子级别在anguler js中过滤列表

时间:2016-09-15 13:35:53

标签: angularjs ng-filter

我从服务器收到了一个像这样的对象列表

{% extends 'rango/base.html' %}
{% load staticfiles %}

{% block title_block %}
    Register
{% endblock %}

{% block body_block %}
    <h1>Register with Rango</h1>
    {% if registered %}
        Rango says: <strong>thank you for registering!</strong>
        <a href="/rango/">Return to the homepage</a><br/>
    {% else %}
        Rango says: <strong>register here!</strong>
        Click <a href="/rango/">here</a> to go to the homepage<br/>
        <form id="user_form" method="post" action="/rango/register/" enctype="multipart/form-data">
            {% csrf_token %}
            {{ user_form.as_p }}
            {{ profile_form.as_p }}

            <input type="submit" name="submit" value="Register" />
        </form>
    {% endif %}
{% endblock %}

现在我想根据“var”过滤此列表。我正在做类似

的事情
[
    {id: 'a', checked :true,test:{sub:{var:4}}}, 
    {id: 'b', checked:true,test:{sub:{var:3}}},
    {id: 'c', checked:true,test:{sub:{var:2}}},
    {id: 'd', checked:true,test:{sub:{var:2}}},
    {id: 'e', checked:true,test:{sub:{var:3}}}
]

我的控制器代码就像

    <div ng-controller="repeatCtrl">
   <div ng:repeat="o in list | filter: {test.sub.var : myPrecious.id}">
       {{o.id}}: 
       <input type="checkbox" ng:model="o.checked" />
       {{o.checked}}
    </div>
</div>

我不知道什么是错的,请帮忙。

1 个答案:

答案 0 :(得分:1)

试试这个solution。您可以在{$ : myPrecious.id}指定filter,而无需担心层次结构级别,如docs.angularjs.org所述:特殊属性名称(默认为$)可以使用(例如在{$中) :“text”})接受对象的任何属性或其嵌套对象属性的匹配:

<强> HTML:

<div ng-repeat="o in list | filter : {$ : myPrecious.id}">
    {{o.id}}
    <input type="checkbox" ng-model="o.checked" />
    {{o.checked}}
</div>

<强>使用Javascript:

$scope.list = [
    {id: 'a', checked:true,test:{sub:{var:4}}}, 
    {id: 'b', checked:true,test:{sub:{var:3}}},
    {id: 'c', checked:true,test:{sub:{var:2}}},
    {id: 'd', checked:true,test:{sub:{var:2}}},
    {id: 'e', checked:true,test:{sub:{var:3}}}
];

$scope.myPrecious = {id:2};
相关问题