& quot; v.context.$ implicit.categories [0]是未定义的& quot;Ionic 3中的错误 [英] "v.context.$implicit.categories[0] is undefined" error in Ionic 3

查看:117
本文介绍了& quot; v.context.$ implicit.categories [0]是未定义的& quot;Ionic 3中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WordPress rest API访问离子产品的类别标签.在< h3 class ="product-name uppercase"> {{productdetail?.categories [0] .slug}}</h3> 上,这似乎可以在一页上很好地工作,但这确实可以当我尝试在另一页上的Angular中实现相同的for循环时,似乎不起作用

I am trying to access the category slug for a product in ionic using the WordPress rest API. This seems to work fine on one page as <h3 class="product-name uppercase">{{productdetail?.categories[0].slug}}</h3> however this does not seems to work when i try implementing the same using for loop in angular on another page

<div class="width50" *ngFor="let object of dataList">
    <img src="{{object.images[0].src}}" width="150"  (click)="navigateToPage(object.id)" />
    <h3 class="product-name uppercase" text-nowrap>{{object?.categories[0].slug}}</h3> 
</div>

由于未定义 v.context.$ implicit.categories [0] ,因此出现错误,但是在同一页面上,图像源代码似乎可以正常工作.

it gives an error as v.context.$implicit.categories[0] is undefined however on same page the code for image source seems to work fine.

推荐答案

根据

Angular安全导航操作符(?.)流畅且方便防止属性路径中的空值和未定义值的方法.这里是的,如果currentHero为空.

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.

这样写就可以

obj?.foo

angular会将其转换为以下表达式.

angular will transform it to the following expression.

obj == null ? null : obj.foo

因此,如果 obj 为空值,则不会看到任何错误.

so if the obj is empty value then you won't see any of errors.

您可以使用表达式

object?.categories[0]?.slug

将被转换为

object == null ? null : object.categories[0] == null ? null : object.categories[0].slug;

如果您不知道对象是否包含任何值,或者异步加载数据并且对象在第一次时是 undefined ,则安全导航运算符会很有帮助.

Safe navigation operator is helpful if you don't know whether your object contains any value or not, or you load data asynchronously and your object is undefined at first time.

这篇关于&amp; quot; v.context.$ implicit.categories [0]是未定义的&amp; quot;Ionic 3中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆