angular 4无法读取未定义的属性“id”

前端之家收集整理的这篇文章主要介绍了angular 4无法读取未定义的属性“id”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用angular 4来调用虚拟web api,这是我的组件

export class AppComponent {
    title = 'app';
    url = 'https://api.ipify.org/?format=json';
     results: any;

 constructor(private http: Http) {

this.getData().subscribe(data => {
  this.results = data.json();

  })
 }

 getData() {
return this.http.get(this.url);
 }

}

但当我尝试显示它时,我得到的结果,但也有一个错误的屏幕

ERROR TypeError: Cannot read property 'ip' of undefined

我想要理解的是为什么它被显示,因为在一瞬间之后数据被很好地显示.

解决方法

在您的* ngFor中,您可以将其更改为* ngFor =“let r of results | async”

您的问题是数据尚未存在,因此无法读取id属性.

如果使用异步管道,它应该在显示数据之前等待数据.

猜你在找的Angularjs相关文章