undefined error while accessing an object from .ts file to .html in ionic 3

拈花ヽ惹草 提交于 2019-12-25 10:01:32

问题


I am trying to access an object value sent via a .ts file on .html file but it shows

"Error: Uncaught (in promise): TypeError: co.productdetail is undefined
View."

here the productdetail is the variable name that i am trying to pass from the .ts file to the .html file.This is the way I am trying to print the name attribute of object in the html file <h3>{{productdetail.name}}</h3>. however following seems to work fine in another file.

<div class="width50" *ngFor="let object of dataList">
<img src="{{object.images[0].src}}" width="150"  (click)="navigateToPage(object.id)" />

here datalist is an array of objects passed from the .ts file to .html file. Kindly suggest a solution to the same.


回答1:


Use typesafe ? operator to avoid this error

<h3>{{productdetail?.name}}</h3>

Reason:

  • DOM elements are rendered even before ngOnInit() is fired.
  • If you are using data from the service response, the bindings will have them as undefined until the response is received.


来源:https://stackoverflow.com/questions/44862258/undefined-error-while-accessing-an-object-from-ts-file-to-html-in-ionic-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!