ERROR TypeError: Cannot read property 'length' of undefined
ERROR TypeError: Cannot read property 'length' of undefined
我的这部分代码有错误
<img src="../../../assets/gms-logo.png" alt="logo" routerLink="/overview" alt="website icon">
但是当我检查资产文件夹时,gms-logo.png
仍然存在,而在 angular-cli.json
中,资产也在那里。路径也是正确的。
虽然最近,我一直在研究搜索功能。所以我的假设是,
Has the program started searching even if the user is still not focused on the input type? How do I fix this?
下面是我的 html 搜索及其建议部分的显示
<input type="text" placeholder="Search" (keyup)="onSearch($event.target.value)">
<div class="suggestion" *ngIf="results.length > 0">
<div *ngFor="let result of results ">
<a href="" target="_blank">
{{ result.name }}
</a>
</div>
</div>
下面是我的组件
results: Object;
onSearch(name) {
this.search
.searchEmployee(name)
.subscribe(
name => this.results = name,//alert(searchName),this.route.navigate(['/information/employees/']),
error => alert(error),
);
}
您需要将 results
变量初始化为数组。
在您的组件中,添加:
results = [];
另一种选择是更改您的建议 div 的 *ngIf
语句以检查 results
是否已定义:
<div class="suggestion" *ngIf="results">
<div *ngFor="let result of results ">
<a href="" target="_blank">
{{ result.name }}
</a>
</div>
</div>
The safe navigation operator ( ?. ) and null property paths
The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths. Here it is, protecting against a view render failure if the currentHero is null.
因此在您的示例中,您还可以使用 安全导航运算符 ( ?. ):
<div class="suggestion" *ngIf="results?.length > 0">
<div *ngFor="let result of results ">
<a href="" target="_blank">
{{ result.name }}
</a>
</div>
</div>
您可以在声明中初始化您的数组:
result: Array<any>;
如果问题样式仍然存在,您应该像这样在您的方法中检查 null :
onSearch(name) {
this.search
.searchEmployee(name)
.subscribe(
name =>
if(name!=null){
this.results =
name,//alert(searchName),this.route.navigate(['/information/employees/']),
}
error => alert(error),
);
}
将变量初始化为空解决了我的问题。
DoctorList: any[] = [];
我遇到了同样的问题。我找到了 2 种修复方法
将结果分配为数组
结果=[]
在html
*ngIf="results.length"
使用 ?
*ngIf="results?.length"
我遇到了类似的情况,即使在将 results
分配为 数组 (如下所示),错误仍然存在。
results: Array<any>;
使用'?' (安全导航操作员) 对我来说效果很好。
*ngIf="results?.length"
安全导航运算符 (?) 可用于防止 Angular 在尝试访问不存在的对象的属性时抛出错误。
仅当 results
的值不是 Null 或 Undefined 时,此示例才会计算 length
.
我的这部分代码有错误
<img src="../../../assets/gms-logo.png" alt="logo" routerLink="/overview" alt="website icon">
但是当我检查资产文件夹时,gms-logo.png
仍然存在,而在 angular-cli.json
中,资产也在那里。路径也是正确的。
虽然最近,我一直在研究搜索功能。所以我的假设是,
Has the program started searching even if the user is still not focused on the input type? How do I fix this?
下面是我的 html 搜索及其建议部分的显示
<input type="text" placeholder="Search" (keyup)="onSearch($event.target.value)">
<div class="suggestion" *ngIf="results.length > 0">
<div *ngFor="let result of results ">
<a href="" target="_blank">
{{ result.name }}
</a>
</div>
</div>
下面是我的组件
results: Object;
onSearch(name) {
this.search
.searchEmployee(name)
.subscribe(
name => this.results = name,//alert(searchName),this.route.navigate(['/information/employees/']),
error => alert(error),
);
}
您需要将 results
变量初始化为数组。
在您的组件中,添加:
results = [];
另一种选择是更改您的建议 div 的 *ngIf
语句以检查 results
是否已定义:
<div class="suggestion" *ngIf="results">
<div *ngFor="let result of results ">
<a href="" target="_blank">
{{ result.name }}
</a>
</div>
</div>
The safe navigation operator ( ?. ) and null property paths
The Angular safe navigation operator (?.) is a fluent and convenient way to guard against null and undefined values in property paths. Here it is, protecting against a view render failure if the currentHero is null.
因此在您的示例中,您还可以使用 安全导航运算符 ( ?. ):
<div class="suggestion" *ngIf="results?.length > 0">
<div *ngFor="let result of results ">
<a href="" target="_blank">
{{ result.name }}
</a>
</div>
</div>
您可以在声明中初始化您的数组:
result: Array<any>;
如果问题样式仍然存在,您应该像这样在您的方法中检查 null :
onSearch(name) {
this.search
.searchEmployee(name)
.subscribe(
name =>
if(name!=null){
this.results =
name,//alert(searchName),this.route.navigate(['/information/employees/']),
}
error => alert(error),
);
}
将变量初始化为空解决了我的问题。
DoctorList: any[] = [];
我遇到了同样的问题。我找到了 2 种修复方法
将结果分配为数组
结果=[]
在html
*ngIf="results.length"
使用 ?
*ngIf="results?.length"
我遇到了类似的情况,即使在将 results
分配为 数组 (如下所示),错误仍然存在。
results: Array<any>;
使用'?' (安全导航操作员) 对我来说效果很好。
*ngIf="results?.length"
安全导航运算符 (?) 可用于防止 Angular 在尝试访问不存在的对象的属性时抛出错误。
仅当 results
的值不是 Null 或 Undefined 时,此示例才会计算 length
.