使用ng-repeat时将'query'应用于json.field

时间:2013-09-11 08:16:25

标签: angularjs pug

代表

$scope.phones = [ 

    {"name": "Nexus S",
 "snippet": "Fast just got faster with Nexus S."},
{"name": "Motorola XOOM™ with Wi-Fi",
 "snippet": "The Next, Next Generation tablet."},
{"name": "MOTOROLA XOOM™",
 "snippet": "The Next, Next Generation tablet."}

];

html(jade)

p Search <input ng-model="query">

以下适用'过滤器|查询'到整个'手机'。如何仅将查询应用于手机中的字段?

喜欢(phones.snippet | filter:query)

li(ng-repeat="phone in phones|filter:query")

1 个答案:

答案 0 :(得分:0)

您可以自己定义过滤器:

function MainController($scope) {
$scope.searchparam = "Nexus";

$scope.query = function(item) {
    return item.name.contains($scope.searchparam);
};
$scope.phones = 
    [ 
        {"name": "Nexus S", "snippet": "Fast just got faster with Nexus S."},
        {"name": "Motorola XOOM™ with Wi-Fi", "snippet": "The Next, Next Generation tablet."},
        {"name": "MOTOROLA XOOM™", "snippet": "The Next, Next Generation tablet."}
    ];}

demo