Angular 2无法读取未定义的属性

时间:2017-04-25 04:04:15

标签: angular lodash

我有一个简单的搜索文本框,并且值已更改我正在调用此函数(我正在使用Lodash)

  onKeyCustomerSearch(event: any) {

    this.SearchCustomerString = event.target.value;

    this.customersView = _.clone(this.customersArray);
    this.customersView.paginatedCustomers = _.filter(this.customersView.paginatedCustomers, function (x) {
      return x.BusinessName.includes(this.SearchCustomerString);
    });
  }

我无法读取未定义的属性'SearchCustomerString'

然而这是有效的!

  onKeyCustomerSearch(event: any) {

    this.customersView = _.clone(this.customersArray);
    this.customersView.paginatedCustomers = _.filter(this.customersView.paginatedCustomers, function (x) {
      return x.BusinessName.includes(event.target.value);
    });
  }

1 个答案:

答案 0 :(得分:2)

我不使用函数表达式(FE),而是使用箭头函数()=>来保留this

this.customersView.paginatedCustomers = _.filter(this.customersView.paginatedCustomers, 
                          (x) => x.BusinessName.includes(this.SearchCustomerString));

另见