是否有像可以在阵列上使用的安全导航操作员?(Is there something like a Safe Navigation Operator that can be used on Arrays?

编程入门 行业动态 更新时间:2024-10-24 02:38:30
是否有像可以在阵列上使用的安全导航操作员?(Is there something like a Safe Navigation Operator that can be used on Arrays?)

我已经使用安全导航运算符来加载异步调用,这非常棒。 我想我可以重现相同的数组,但它在我的Angular代码中显示模板解析错误。 我知道*ngIf是一种替代解决方案,但是与安全导航操作员一样,是否有更简单的(通过代码)方式?

<div class="mock"> <h5>{{data?.title}}</h5> //This works <h6>{{data?.body}}</h6> //This works <h6>{{simpleData?[0]}}</h6> // This is what I tried to implement </div>

I have used Safe Navigation Operator for Objects to load on Asynchronous calls and it is pretty amazing. I thought I could reproduce the same for Arrays but it displays a template parse error in my Angular code. I know *ngIf is an alternative solution, but is there a more simpler(by code) way just like the Safe Navigation Operator?

<div class="mock"> <h5>{{data?.title}}</h5> //This works <h6>{{data?.body}}</h6> //This works <h6>{{simpleData?[0]}}</h6> // This is what I tried to implement </div>

最满意答案

是否有更简单的(通过代码)方式就像安全导航操作员一样?

有三元操作符 。

条件? expr1:expr2

<h6>{{simpleData?simpleData[0]:''}}</h6>

is there a more simpler(by code) way just like the Safe Navigation Operator?

There is ternary operator.

condition ? expr1 : expr2

<h6>{{simpleData?simpleData[0]:''}}</h6>

更多推荐

本文发布于:2023-08-07 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465064.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作员   阵列   Safe   Arrays   Operator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!