【问题标题】:toLowerCase does not exist on type. Typescript类型上不存在 toLowerCase。打字稿
【发布时间】:2018-07-17 20:19:24
【问题描述】:

我想为自动完成材料创建过滤器。

我的模特:

 export class Country {
      country_id: number;
      name: string;
       }

调用 webmethod ws

 this.ws.AllCountry().subscribe(
      countryes => {
        this.countryes = countryes.map((country) => {
          return new Country(country);
        });
      }
    );

创建此过滤器,但不起作用:

filterStates(val: string) {
    if (val) {
      let filterValue = val.toLowerCase();//toLowerCase does not exist on type Country.
      return this.countryes.filter(name => name.toLowerCase().startsWith(filterValue));
    }
    return this.countryes;
  }

您能建议我任何解决方案吗?

谢谢

【问题讨论】:

  • 你怎么打电话给filterStates

标签: angular typescript filter autocomplete angular-material


【解决方案1】:

如果您在 Angular 2 中使用 jquery,请使用 toString 函数

$('#search').val().toString().toLowerCase()

【讨论】:

    【解决方案2】:
        filterStates(val: string) {
        if (val) {
          let filterValue = val.toLowerCase();//toLowerCase does not exist on type Country.
          return this.countryes.filter(item => item.name.toLowerCase().startsWith(filterValue));
        }
        return this.countryes;
      }
    

    您使用的是 country 对象而不是 name 属性。

    【讨论】:

      【解决方案3】:

      我认为您的错误发生在过滤器行上,因为您在 Country 对象本身上调用 toLowerCase。试试这个:

      filterStates(val: string) {
          if (val) {
            let filterValue = val.toLowerCase();
      
            // the below line is the change
            return this.countryes.filter(country => country.name.toLowerCase().startsWith(filterValue));
          }
          return this.countryes;
        }
      

      【讨论】:

        猜你喜欢
        • 2017-03-02
        • 2021-09-07
        • 2021-07-24
        • 2017-08-07
        • 2017-03-26
        • 2017-09-06
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多