错误类型错误:无法读取未定义的属性“长度" [英] ERROR TypeError: Cannot read property 'length' of undefined

查看:41
本文介绍了错误类型错误:无法读取未定义的属性“长度"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这部分代码有错误

<img src="../../../assets/gms-logo.png" alt="logo" routerLink="/overview" alt="网站图标">

但是当我检查资产文件夹时,gms-logo.png 仍然存在,并且在 angular-cli.json 中,资产也在那里.路径也是正确的.

最近,我一直在研究搜索功能.所以我的假设是,

<块引用>

即使用户仍然没有关注输入类型,程序是否已经开始搜索?我该如何解决这个问题?

下面是我的搜索 html 及其建议段的显示

<div class="suggestion" *ngIf="results.length > 0"><div *ngFor="让结果的结果"><a href="" target="_blank">{{ result.name }}</a>

下面是我的组件

结果:对象;onSearch(名称){this.search.searchEmployee(姓名).订阅(名称 =>this.results = name,//alert(searchName),this.route.navigate(['/information/employees/']),错误 =>警报(错误),);}

解决方案

您需要将 results 变量初始化为数组.

在您的组件中,添加:

results = [];

另一种选择是更改您的建议 div 的 *ngIf 语句以检查是否定义了 results:

<div *ngFor="让结果的结果"><a href="" target="_blank">{{ result.name }}</a>

There is an error in this part of my code

<img src="../../../assets/gms-logo.png" alt="logo" routerLink="/overview" alt="website icon">

But when I checked the assets folder, gms-logo.png is still there and in angular-cli.json, assets is also there. The path is also correct.

Recently though, I've been working on Search function. So my hypothesis is,

Has the program started searching even if the user is still not focused on the input type? How do I fix this?

Below is my html for Search and the showing of its suggestion segment

<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>

Below is my component

results: Object;



 onSearch(name) {
            this.search
            .searchEmployee(name)
            .subscribe(
                name => this.results = name,//alert(searchName),this.route.navigate(['/information/employees/']),
                error => alert(error),
                );
}

解决方案

You need to initialize your results variable as an array.

In your component, add:

results = [];

Another option is to change your suggestion div's *ngIf statement to check if results is defined:

<div class="suggestion"  *ngIf="results">
   <div *ngFor="let result of results ">
          <a href="" target="_blank">
                {{ result.name }}
       </a>
   </div>
</div>

这篇关于错误类型错误:无法读取未定义的属性“长度"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆