如何使用ng-repeat显示具有条件的json数组?

时间:2015-09-15 06:38:01

标签: javascript json angularjs

嗨,我有一个像这样的json数组

var homes = [
    {

        "city": "Dallas",
        "state": "TX",
        "zip": "75201",
        "NoteNumber": "162500"
    }, {

        "city": "Bevery Hills",
        "state": "CA",
        "zip": "90210",
        "NoteNumber": "319250"
    }, {

        "city": "New York",
        "state": "NY",
        "zip": "00010",
        "NoteNumber": "null"
    }
];

我使用div

ng-repeat中显示它们
<div ng-repeat="name in homes">
{{name.city}}
</div>

我有html select标记,其中包含选项notenumbernull

如果选择了notenumber,请告诉我如何显示只有notenumber的城市,如果选择为null,则只显示null个非城市。

2 个答案:

答案 0 :(得分:1)

使用绑定到SELECT

的另一个范围var过滤数组
<select ng-model="selectedNoteNumber">YOUR OPTIONS HERE</select>

<div ng-repeat="name in homes | filter: { 'NoteNumber': selectedNoteNumber }">
    {{name.city}}
</div>

答案 1 :(得分:0)

你可以试试这个

<select ng-model="NoteNumber" ng-options="homes.NoteNumber as homes.NoteNumber for NoteNumber in homes"> </select>

<div ng-repeat="name in homes">
    <span ng-if="NoteNumber !== 'null'">{{name.city}}</span>
</div>

希望这有帮助