Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【Angular2】Uncaught (in promise): TypeError: Cannot read property 'id'大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

背景

需要显示用户信息,用到了插值表达式和双向绑定
开始数据显示正常,但是浏览器一直报错,不知为何

因为可以正常运行,所以就继续做
但是用到后台返回值来切换编辑状态的时候,什么都不显示
所以就得解决了这个问题

问题

ERROR Error: Uncaught (in promisE): TypeError: CAnnot read property ‘id’ of undefined

TypeError: CAnnot read property ‘id’ of undefined

解决

1.使用箭头函数

getstudents():void{
    this.studentservice.getstudents().then(students=>this.students=students);
  }

2.在插值表达式中加入问号

{{student?.name}} //not {{student.name}}

说明:

因为在我们加载对象的时候,用的是异步模式,即使promise立刻被处理返回,但是浏览器在开始加载对象的时候,这个对象还是没有定义,所以也就读不到属性

https://stackoverflow.com/questions/38078741/angularjs-2-exception-error-uncaught-in-promise-typeerror-cannot-set-prope

@H_489_68@

大佬总结

以上是大佬教程为你收集整理的【Angular2】Uncaught (in promise): TypeError: Cannot read property 'id'全部内容,希望文章能够帮你解决【Angular2】Uncaught (in promise): TypeError: Cannot read property 'id'所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。