gpt4 book ai didi

javascript - AngularJS 中带有对象的 startWith 过滤器

转载 作者:行者123 更新时间:2023-12-02 14:55:07 24 4
gpt4 key购买 nike

我正在尝试使用 startWith 过滤器,但它没有正确过滤数据,就像 P 的情况一样。让我知道我在这里做错了什么。

Controller 代码 -

var app = angular.module('filterSample', []);app.controller('filterCtrl', 函数 ($scope) {

$scope.data = {
messages: [
{
"id":"111619EEVz",
"name":"Tom 67",
"status":"Pending"
},
{
"id":"115419EEAA",
"name":"Business 34",
"status":"Pending"
},
{
"id":"1B167000WW",
"name":"Jack 78",
"status":"Active",
}
]
}

$scope.startsWith = function (actual, expected) {
var lowerStr = (actual + "").toLowerCase();
return lowerStr.indexOf(expected.toLowerCase()) === 0;
}
});

HTML -

<div ng-controller="filterCtrl">
<input type="text" ng-model="search" />
<ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
<li>{{msg.name}}</li>
</ul>
</div>

工作 Plnkr - http://plnkr.co/edit/tByYZ3jpEk7YPpJnMY11?p=preview

编辑 1 -

过滤应该就像只在 name 属性中搜索,而不是在整个对象中搜索。

最佳答案

将 ng-model 更改为 search.name

<div ng-controller="filterCtrl">
<input type="text" ng-model="search.name" />
<ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
<li>{{msg.name}}</li>
</ul>
</div>

关于javascript - AngularJS 中带有对象的 startWith 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com