toLowerCase在类型上不存在.打字稿

A. *_*Bin 1 autocomplete filter typescript angular-material angular

我想为自动完成素材创建过滤器.

我的模特:

 export class Country {
      country_id: number;
      name: string;
       }
Run Code Online (Sandbox Code Playgroud)

打电话给webmethod ws

 this.ws.AllCountry().subscribe(
      countryes => {
        this.countryes = countryes.map((country) => {
          return new Country(country);
        });
      }
    );
Run Code Online (Sandbox Code Playgroud)

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

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;
  }
Run Code Online (Sandbox Code Playgroud)

你能告诉我任何解决方案吗?

谢谢

Dan*_*n D 6

我认为你的错误是发生在过滤器上线,因为您呼叫toLowerCase的对Country对象本身.试试这个:

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;
  }
Run Code Online (Sandbox Code Playgroud)