为什么我的AngularJS无法正常工作,它是我的过滤器功能吗?

时间:2019-01-18 02:03:24

标签: angularjs filter angular-ui-bootstrap angularjs-filter

我正在尝试将我的ng-repeat与过滤器一起使用。它没有按我希望的那样工作。我想知道是否不可能做我想做的事情?? 我的html:

<input type="text" ng-model="search.text" class="form-control" id="search" placeholder="Search">

<div class='col-md-12' ng-repeat='(key,value) in cropplans | contains:search.text'> <h4> Title: key</h4>
</div>

这是我的控制器和过滤器:

aggriApp.filter('contains', function() {
  return function(values, field) {
        var result = {};
  field = field.toLowerCase()
  angular.forEach(values, function(key, value) {
      if(value.company_name.toLowerCase().indexOf(field) >= 0){
           result[key] = value
            }
        });
  return result

编辑:当我在搜索输入中键入内容时,我所应用的过滤器没有任何更改。如果搜索不匹配,我应该得到0条结果吗?

编辑: $scope.cropplans是一个类似于以下内容的对象:

Object
AkshaYagna: Array(1)
0:
$$hashKey: "object:2684"
active: true
area: "0.00000"
block: {id: 230, name: "default", farm: {…}}
certification: null
closing_reason: null
company_name: "AkshaYagna"
created: "2019-01-17T21:04:23.605311Z"
crop_forecast: [{…}]
estimated_cases_count: 2000
estimated_harvest_date: "2019-01-17"
estimated_harvest_date_end: "2019-02-28"
foodhub: 409
foodhub_seller: 3752
foodhub_transport_time: 0
id: 828
modified: "2019-01-17T21:04:23.605319Z"
planting_date: null
plu_code: {id: 12891, commodity: "Conventional Baby Bok Choy", variety: "Shanghai", size: "30#", package: "Cartons", …}
received_cases: null
received_on: null
user: {id: 4022, email: "asus@lenovo.com", first_name: "Child.farm", last_name: "06", telephone: null, …}
__proto__: Object
received: 0
length: 1
__proto__: Array(0)
Child.farm03: [{…}, received: 0]
Samuel Gibbs: (3) [{…}, {…}, {…}, received: 0]
shadow test: (2) [{…}, {…}, received: 0]
__proto__: Object

1 个答案:

答案 0 :(得分:0)

对于按公司名称过滤数组,无需编写自己的过滤器,可以对对象使用angularjs过滤器过滤器。尝试以下

<input type="text" ng-model="search.text" class="form-control" id="search" placeholder="Search">

<div class='col-md-12' ng-repeat="(key,value) in cropplans | filter : {'company_name' : search.text}"> 
    <h4> {{value}}</h4>
</div>

它不区分大小写,因此您不必担心转换为小写/大写字母。