javascript - 类型错误 : Cannot read property 'toLocaleLowerCase' of undefined

标签 javascript angular typescript angular-pipe

我正在尝试创建一个自定义管道,我已经正确地遵循了说明,但是当我尝试过滤我的列表时,它一直给我这个错误

这是我的管道代码

import { Pipe, PipeTransform } from '@angular/core';
import { Icandidat } from './candidat/icandidat';

@Pipe({
    name :'personFilter'
})

export class PipeFilter implements PipeTransform{


    transform(value: Icandidat[], filterBy : string) : Icandidat[] {
        filterBy = filterBy ? filterBy.toLocaleLowerCase(): null;
        return filterBy? value.filter((person : Icandidat)=>
            person.candidatNom.toLocaleLowerCase().indexOf(filterBy) ! ==-1) : value; 
     }
}

这是我的界面

export interface Icandidat {
    prog1 : string ;
    progName1 : string ;
    progEl1 : string ;
    candInfo : any [];
    filterBy : string ;

    candidatID : number;
    candidatNom : string;
    canditatPrenom : string ;
    candidatParti : string;
    candidatDepartement : string;
    candidatCommune : string ;

    candidats : Icandidat;
    errorMessage : string;


}

我的组件

import { PaeServiceService } from '../pae-service.service';
import { Icandidat } from './icandidat';
import { NgModel } from '@angular/forms/src/directives';
import { Component, OnInit } from '@angular/core';


@Component({
  selector: 'app-candidat',
  templateUrl: './candidat.component.html',
  styleUrls: ['./candidat.component.css'],
  providers : [PaeServiceService]
})
export class CandidatComponent implements OnInit {
    prog1 : string ="Programme d'Appui aux Elections";
    progName1 : string ="Enquête sur les candidats";
    progEl1 : string ="Listes des candidats ciblés";
    candInfo : any [];
    filterBy : string ="Ronny";

    candidatID : number;
    candidatNom : string;
    canditatPrenom : string ;
    candidatParti : string;
    candidatDepartement : string;
    candidatCommune : string ;

    candidats : Icandidat;
    errorMessage : string;

    constructor (private _candidatService : PaeServiceService){

    }

    ngOnInit(): void {
      this._candidatService.getCandidatInfo()
            .subscribe(candidats => this.candInfo = candidats,
            error => this.errorMessage = <any>error);
    }


}

和我的模板

 <table class="bordered highlight" *ngIf='candInfo && candInfo.length'>
                <thead>
                  <tr >
                      <th>Nom</th>
                      <th>Prénom</th>
                      <th>Parti Politique</th>
                      <th>Département</th>
                      <th>Commune</th>
                  </tr>
                </thead>

                <tbody>
                  <tr *ngFor = 'let candidats of candInfo | personFilter : filterBy'>
                    <td>{{candidats.nom}}</td>
                    <td>{{candidats.prenom}}</td>
                    <td>{{candidats.parti}}</td>
                    <td>{{candidats.departement}}</td>
                    <td>{{candidats.commune}}</td>
                  </tr>

                </tbody>
          </table>

知道是什么原因造成的吗?

最佳答案

这里的间距可能是问题所在:

.indexOf(filterBy) ! ==-1)

应该是:

.indexOf(filterBy) !== -1)

请注意,爆炸和双等号之间没有空格。

关于javascript - 类型错误 : Cannot read property 'toLocaleLowerCase' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44661186/

相关文章:

javascript - 处理路由时出错 : Cannot read property 'connectOutlet' in Ember. js

javascript - 从 Angular Controller 调用 webview 方法

javascript - Angular 通用获取错误 : You must pass in a NgModule or NgModuleFactory to be bootstrapped

angular - 使用 Angular 2.0 路由器和组件接口(interface) promise 的页面转换动画

javascript - 在 javascript 中读取本地 xls/xlsx 文件

正则表达式拆分期间出现javascript未定义元素

asynchronous - Angular 2 - 使用异步 http 请求进行插值和绑定(bind)

javascript - 在 Angular 中获取祖先路由参数?

typescript - getter 和 setter 具有不同类型的索引签名

typescript - 如何将 Twix (momentjs) 与 Typescript 结合使用