0

html(玉)包含

body(ng-controller='myAppController')

    <input ng-model="inputQuery">

    ul
        li(ng-repeat="phone in phones|filter:query") {{phone.name}}
            p {{phone.snippet}}

控制器定义为:

function myAppController($scope){


        $scope.query = function(item){
            return item.name.contains('$scope.inputQuery') ;

        }

$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."}

];
}

输出应该是“电话”,其“名称”包含“输入”字母。但是没有输出。(根据输入也没有变化)

经检查,以下工作:

$scope.query = function(item){
        return item.name; // or return item

    }

但即使return item.name.contains('N');不起作用

4

2 回答 2

1

您可以indexOf()检查字符串是否是子字符串

$scope.query = function (item) {
    if ($scope.inputQuery === undefined || $scope.inputQuery === "" ) return item;
    return item.name.indexOf($scope.inputQuery) >= 0;
}

Working Demo

于 2013-09-11T14:02:44.827 回答
0
$scope.query = function (item) {
    return item.name.indexOf($scope.inputQuery || "") >= 0;
}

修改了@sza 的答案以将 $scope 添加到 inputQuery。应该使用评论,但我还无法发表评论。|| ""如果您希望在加载页面时显示列表(在输入输入框之前)。

http://jsfiddle.net/s5Pd7/1/

于 2013-09-11T14:21:33.073 回答