离子过滤器未定义

时间:2018-03-19 11:33:21

标签: angularjs ionic2

我正在使用离子。我正在从数据提供者加载数据。但它显示我的错误无法读取属性' filter'在CategoriesProvider中未定义。也许是因为当我调用filterCategory数据时没有设置,所以我该如何解决这个错误。抱歉我的英语不好。

页:

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Http } from '@angular/http';
import { CategoriesProvider } from '../../providers/categories/categories';
import 'rxjs/add/operator/map';

@IonicPage()
@Component({
  selector: 'page-categories',
  templateUrl: 'categories.html',
})
export class CategoriesPage {

  categories: any;

  Loader: any = 1;

  searchTerm: string = '';

  ion_content = 'page_blue';

  constructor(public navCtrl: NavController, public navParams: NavParams, public http: Http, public dataService: CategoriesProvider) {
  }

  ionViewDidLoad() {
    this.setFilteredCategory();
    console.log('ionViewDidLoad CategoriesPage');
  }

    setFilteredCategory() {
        this.categories = this.dataService.filterCategory(this.searchTerm);
    }

}

提供者:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class CategoriesProvider {

  categories: any;

  constructor(public http: HttpClient) {}

  get_data(searchTerm){
    this.http.get('https://www.example.com/Android/get_category').subscribe(data => {
        this.categories = data;
        this.filterCategory(searchTerm)
    });
  }

    filterCategory(searchTerm){
        return this.categories.filter((category) => {
            return category.English_name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
        });    
    }

}

1 个答案:

答案 0 :(得分:0)

像这样修改你的filterCategory:

filterCategory(searchTerm){
    if(this.categories != null || this.categories != undefined){
        return this.categories.filter((category) => {
            return category.English_name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1;
        });    
    }
}