参见英文答案 >
Angular 2: TypeError: l_thing0 is undefined in [{{thing.title}} in AppComponent@4:44] 3个
我试图在组件的模板中打印一个值.
我试图在组件的模板中打印一个值.
我已从此问题的代码中删除了额外的内容.我想访问详细信息的第一行.
组件文件
import { Component } from '@angular/core'; import { Person } from './person'; import { getPersonDetailsService } from './person.service'; @Component({ selector: 'my-app',template: ` {{data[0].name}} ` }) export class AppComponent { data: Person[] = []; ngOnInit(): void { this.getPersonDetailsService.getData() .then(data => this.data = data.slice(1,5)); } }
解决方法
我认为您应该首先检查在浏览器中打印数据时是否定义了数据.
{{data[0]?.name}}
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.
您还可以使用&& ;;检查变量是否为null.条件
{{data [0]&&数据[0].名称}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。