【发布时间】:2021-04-05 22:10:54
【问题描述】:
Component.ts
branches: any = [];
branchName: string; // any
ngOnInit(): void {
// this.refreshBranchList();
this.service.getBranchList().subscribe((response: any) => {
this.branches = response;
});
}
Search() {
if (this.branchName !== "") {
this.branches = this.branches.filter((res: { branchName: string; }) => {
return res.branchName.toLocaleLowerCase().match(this.branchName.toLocaleLowerCase());
})
} else if (this.branchName == "") {
this.ngOnInit();
}
}
Component.html
<div class="d-flex float-right">
<input type="text" class="form-control" placeholder="Search here" [(ngModel)]="branchName" (ngModelChange)="Search()">
</div>
它正在按预期搜索值。但是控制台中有一个我不熟悉的错误。有什么我想念的请告诉我。
【问题讨论】:
-
错误是自己说的。
res.branchName或this.branchName未定义,并且无法在其中找到函数.toLocaleLowerCase()。尝试在调用.toLocaleLowerCase()之前添加检查值是否存在。
标签: angular typescript django-rest-framework