是否有像可以在阵列上使用的安全导航操作员?(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>是否有像可以在阵列上使用的安全导航操作员?(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>