angular *ngIf- 检查特定索引中的数组是否为空

angular *ngIf- check if array in specific index null

我想检查特定索引中的数组是否不为空 这是我的代码:

    *ngIf="routeService?.selectedPlaces?[index]!=null"

'routeService'是组件在其构造函数中获取的服务,'selectedPlaces'是服务中的数组。 'index'在组件中是属性。

我收到这个错误:

    main.ts:13 Error: Template parse errors:
    Parser Error: Conditional expression routeService?.selectedPlaces?[index]!=null requires all 3 
    expressions at the end of the expression [routeService?.selectedPlaces?[index]!=null] in 
    ng:///PlaceAutocompleteFromDBComponent/template.html@5:29 ("-field class="example-full-width">

如何查看?

在 Typescript 中,elvis operator 语法是 ?. 在第二种情况下,您只使用了 ?,这是无效的。 您需要使用

*ngIf="routeService?.selectedPlaces && routeService.selectedPlaces[index]"